Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Role Select menu #5

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ verify_ssl = true
[dev-packages]

[packages]
nextcord = "==2.0.0a3"
toml = "*"
py-cord = {git = "https://github.com/Pycord-Development/pycord.git", ref = "3ce83f3089dd2d88856858272344f12453613183"}

[requires]
python_version = "3.8"
228 changes: 177 additions & 51 deletions Pipfile.lock

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion example.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ description = "Moderation bot for The Balance"
token = "a discord token"
guild = "a server ID"

[db]
db_name = "a PSQL database name"

[channels]
warning = "a warning channel ID"
log = "a log channel ID"
13 changes: 10 additions & 3 deletions libra/bot.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import asyncio
import discord
from discord.ext import commands
from discord_ui import UI
import toml

# Cogs that should run on startup
default_cogs = ['cogs.moderation']
default_cogs = [
'cogs.moderation',
'cogs.roles',
]

class Libra(commands.Bot):
def __init__(self, config):
Expand All @@ -14,11 +17,15 @@ def __init__(self, config):
async def on_ready(self):
print(f"Logged in as: {self.user.name}")

async def log_message(self, embed: discord.Embed):
channel_id = self.config['channels']['log']
channel = self.get_channel(channel_id)
await channel.send(embed=embed)


async def run():
config = toml.load("config.toml")
bot = Libra(config)
ui = UI(bot)

if __name__ == '__main__':
for cog in default_cogs:
Expand Down
45 changes: 19 additions & 26 deletions libra/cogs/moderation.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,41 @@
from datetime import datetime
import discord
from discord.ext import commands
from discord_ui.cogs import slash_cog
from discord.commands import slash_command

class ModerationCog(commands.Cog):
def __init__(self, bot):
self.bot = bot

@slash_cog(name="ban")
@slash_command(name="ban", description="Ban a user.")
@commands.guild_only()
@commands.has_permissions(ban_members=True)
async def ban(self, ctx, user: discord.Member, reason: str):
userID = (user.id)
async def ban(self, ctx: commands.Context, user: discord.Member, reason: str):
embed = discord.Embed(title="Member Banned", color = 0xD82626)
embed.add_field(name="Member", value="{} ".format(user) + "(<@{}>)".format(userID), inline=True)
embed.add_field(name="Mod", value="{}".format(ctx.author.nick), inline=True)
embed.add_field(name="Reason", value="{}".format(reason), inline=False)
embed.set_thumbnail(url=user.avatar_url)
embed.add_field(name="Member", value=f"<@{user.id}> ({user})", inline=True)
embed.add_field(name="Mod", value=ctx.author.nick, inline=True)
embed.add_field(name="Reason", value=reason, inline=False)
embed.set_thumbnail(url=user.display_avatar)
embed.timestamp = datetime.utcnow()

log_channel_id = self.bot.config['channels']['log']
log_channel = self.bot.get_channel(log_channel_id)

await user.ban(reason=reason)
await log_channel.send(embed=embed)
await ctx.respond(embed=embed)
await self.bot.log_message(embed=embed)
await ctx.respond(embed=embed, ephemeral=True)

@slash_cog(name="kick")
@slash_command(name="kick", description="Kick a user.")
@commands.guild_only()
@commands.has_permissions(kick_members=True)
async def kick(self, ctx, user: discord.Member):
userID = (user.id)
async def kick(self, ctx: commands.Context, user: discord.Member):
embed = discord.Embed(title="Member Kicked", color = 0xD82626)
embed.add_field(name="Member", value="{} ".format(user) + "(<@{}>)".format(userID), inline=True)
embed.add_field(name="Mod", value="{}".format(ctx.author.nick), inline=True)
embed.set_thumbnail(url=user.avatar_url)
embed.add_field(name="Member", value=f"<@{user.id}> ({user})", inline=True)
embed.add_field(name="Mod", value=ctx.author.nick, inline=True)
embed.set_thumbnail(url=user.display_avatar)
embed.timestamp = datetime.utcnow()

log_channel_id = self.bot.config['channels']['log']
log_channel = self.bot.get_channel(log_channel_id)

await user.kick()
await log_channel.send(embed=embed)
await ctx.respond(embed=embed)

await self.bot.log_message(embed=embed)
await ctx.respond(embed=embed, ephemeral=True)


def setup(bot):
bot.add_cog(ModerationCog(bot))
bot.add_cog(ModerationCog(bot))
76 changes: 76 additions & 0 deletions libra/cogs/roles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
from typing import Dict
import discord
from discord.ext import commands
from discord.commands import slash_command

class RoleButton(discord.ui.Button):
r"""A discord UI button which toggles a server role for a user.

Attributes
----------
label: str
The button's label text.
role: :class:`discord.Role`
The role this button should toggle.
emoji: :class:`discord.Emoji`
The button's emoji.
row: int
The row this button should be displayed in within the view.
"""

def __init__(self, label: str, role: discord.Role, emoji: discord.Emoji, row: int):
super().__init__(style=discord.ButtonStyle.blurple, label=label, emoji=emoji, row=row)
self.id = id
self.role = role

async def callback(self, interaction: discord.Interaction):
user = interaction.user

if self.role in user.roles:
await user.remove_roles(self.role)
else:
await user.add_roles(self.role)


class RoleSelectView(discord.ui.View):
r"""Defines the Discord interaction view for the Role Select command.

Attributes
----------
roles: Dict[str, :class:`discord.Role`]
A dict of server roles keyed by their name.
emojis: Dict[str, :class:`discord.Emoji`]
A dict of server emojis keyed by their name.
"""

def __init__(self, roles: Dict[str, discord.Role], emojis: Dict[str, discord.Emoji]):
super().__init__()
# Some example buttons, we'll want to store the role name / emoji name in a DB
self.add_item(RoleButton("Bard", roles['Bard'], emojis['bard'], 0))
self.add_item(RoleButton("Machinist", roles['Machinist'], emojis['machinist'], 1))


class RolesCog(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.emojis = {}
self.roles = {}

@slash_command(name="roleselect", description="Opens a menu to add/remove server roles.")
@commands.guild_only()
async def roleselect(self, ctx: commands.Context):
if not self.emojis:
# Build a dict from the server's emojis if it doesn't exist yet
for emoji in ctx.guild.emojis:
self.emojis[emoji.name] = emoji

if not self.roles:
# Build a dict from the server's roles if it doesn't exist yet
for role in await ctx.guild.fetch_roles():
self.roles[role.name] = role

await ctx.respond("**Choose your roles**", view=RoleSelectView(self.roles, self.emojis), ephemeral=True)


def setup(bot):
bot.add_cog(RolesCog(bot))
3 changes: 0 additions & 3 deletions requirements.txt

This file was deleted.