diff --git a/examples/interactionsBot/bot.py b/examples/interactionsBot/bot.py deleted file mode 100644 index 3e7cf90525..0000000000 --- a/examples/interactionsBot/bot.py +++ /dev/null @@ -1,44 +0,0 @@ -""" -with slash commands user permission it's better to check with property -""" - -import os - -import discord -from discord.ext import commands -from discord.ui.view import View - - -# inherits commands.Bot -class BotClass(discord.Bot): - def __init__(self): - super().__init__() - self.persistent_views_added = False - - # For making the intreaction Button works even after restart. - async def on_ready(self): - if not self.persistent_views_added: - - # You can add classes to the to make it work after restart - # self.add_view() - - print(f"Connected as {self.user} with ID {self.user.id}") - print("------") - self.persistent_views_added = True - - -bot = BotClass() - -for filename in os.listdir("./cogs"): - if filename.endswith(".py"): - bot.load_extension(f"cogs.{filename[:-3]}") - -# using SlashCommandGroup -import cog_group - -cog_group.addition() -# subtraction method is called last because of inside it. -# calling subtraction first then addition would most likely not work and won't register addition slash command. -cog_group.subtraction(bot) - -bot.run("token") diff --git a/examples/interactionsBot/cog_group/__init__.py b/examples/interactionsBot/cog_group/__init__.py deleted file mode 100644 index 25531ed021..0000000000 --- a/examples/interactionsBot/cog_group/__init__.py +++ /dev/null @@ -1,37 +0,0 @@ -""" -This init file will be called in bot.py before or after of cog import. -client: or should be present for the last method defined here -for method. - -do note that the client.add_application_command should only be called in only one method and that method -should be called in the very last in the bot.py -""" - -import discord -from discord.commands.commands import Option, SlashCommandGroup -from discord.ext.commands.context import Context - -MathGroup = SlashCommandGroup("math", "maths!.", guild_ids=[...]) - - -def addition(): # you can use whatever parameters needed for you command. - # the main decorator will be called inside the method. - @MathGroup.command(name="add", guild_ids=[...], description="addition!") - async def add( - ctx: Context, - number1: Option(int, "first integer"), # refer to slashOption.py - number2: Option(int, "second integer"), # refer to slashOption.py - ): - await ctx.respond(f"{number1}+{number2}={number1+number2}") - - -def subtraction(client): - @MathGroup.command(name="subtract", guild_ids=[...], description="subtraction!") - async def sub( - ctx: Context, - number1: Option(int, "first integer"), # refer to slashOption.py - number2: Option(int, "second integer"), # refer to slashOption.py - ): - await ctx.respond(f"{number1}-{number2}={number1-number2}") - - client.add_application_command(MathGroup) diff --git a/examples/interactionsBot/cogs/basicSlash_cog.py b/examples/interactionsBot/cogs/basicSlash_cog.py deleted file mode 100644 index 6ca7df59bd..0000000000 --- a/examples/interactionsBot/cogs/basicSlash_cog.py +++ /dev/null @@ -1,30 +0,0 @@ -import discord -from discord.commands import Option, slash_command -from discord.ext import commands -from discord.ext.commands.context import Context - - -class SlashExample(commands.Cog): - def __init__(self, bot): - self.bot = bot - - @slash_command( - guild_ids=[...], name="ping", description="check the latency of the bot!" - ) - async def ping(self, ctx): - """ - ephemeral makes "Only you can see this" message - - `await ctx.respond(f"{round(self.client.latency * 1000)}ms",ephemeral=True)` - """ - return await ctx.respond(f"{round(self.client.latency * 1000)}ms") - - @ping.error - async def ping_error(self, ctx: Context, error): - return await ctx.respond( - error, ephemeral=True - ) # ephemeral makes "Only you can see this" message - - -def setup(bot): - bot.add_cog(SlashExample(bot)) diff --git a/examples/interactionsBot/cogs/button_cog.py b/examples/interactionsBot/cogs/button_cog.py deleted file mode 100644 index 2d50b8b887..0000000000 --- a/examples/interactionsBot/cogs/button_cog.py +++ /dev/null @@ -1,42 +0,0 @@ -import discord -from discord.commands import slash_command -from discord.ext import commands -from discord.ext.commands.context import Context - - -class ButtonView(discord.ui.View): - def __init__(self): - # making None is important if you want the button work after restart! - super().__init__(timeout=None) - - # custom_id is required and should be unique for - # attribute emoji can be used to include emojis which can be default str emoji or str(<:emojiName:int(ID)>) - # timeout can be used if there is a timeout on the button interaction. Default timeout is set to 180. - @discord.ui.button( - style=discord.ButtonStyle.blurple, custom_id="counter:firstButton" - ) - async def leftButton(self, button, interaction): - await interaction.response.edit_message("button was pressed!") - - -class ButtonExample(commands.Cog): - def __init__(self, bot): - self.bot = bot - - @slash_command( - guild_ids=[...], name="slash_command_name", description="command description!" - ) - async def CommandName(self, ctx): - navigator = ButtonView() # button View - await ctx.respond("press the button.", view=navigator) - - # for error handling - @CommandName.error - async def CommandName_error(self, ctx: Context, error): - return await ctx.respond( - error, ephemeral=True - ) # ephemeral makes "Only you can see this" message - - -def setup(bot): - bot.add_cog(ButtonExample(bot)) diff --git a/examples/interactionsBot/cogs/dropdown_cog.py b/examples/interactionsBot/cogs/dropdown_cog.py deleted file mode 100644 index 3e74b9b1c1..0000000000 --- a/examples/interactionsBot/cogs/dropdown_cog.py +++ /dev/null @@ -1,77 +0,0 @@ -import discord -from discord.commands import slash_command -from discord.ext import commands -from discord.ext.commands.context import Context - - -# Defines a custom Select containing colour options -# that the user can choose. The callback function -# of this class is called when the user changes their choice -class Dropdown(discord.ui.Select): - def __init__(self): - - # Set the options that will be presented inside the dropdown - options = [ - discord.SelectOption( - label="Red", description="Your favourite colour is red", emoji="🟥" - ), - discord.SelectOption( - label="Green", description="Your favourite colour is green", emoji="🟩" - ), - discord.SelectOption( - label="Blue", description="Your favourite colour is blue", emoji="🟦" - ), - ] - - # The placeholder is what will be shown when no option is chosen - # The min and max values indicate we can only pick one of the three options - # The options parameter defines the dropdown options. We defined this above - super().__init__( - placeholder="Choose your favourite colour...", - min_values=1, - max_values=1, - options=options, - ) - - async def callback(self, interaction: discord.Interaction): - # Use the interaction object to send a response message containing - # the user's favourite colour or choice. The self object refers to the - # Select object, and the values attribute gets a list of the user's - # selected options. We only want the first one. - await interaction.response.send_message( - f"Your favourite colour is {self.values[0]}" - ) - - -class DropdownView(discord.ui.View): - def __init__(self): - super().__init__() - - # Adds the dropdown to our view object. - self.add_item(Dropdown()) - - -class DropdownExample(commands.Cog): - def __init__(self, bot): - self.bot = bot - - @slash_command( - guild_ids=[...], name="color", description="tell me your favourite color!" - ) - async def whatcolor(self, ctx): - await ctx.respond("what is your favrouite color?", view=DropdownView()) - - # ephemeral makes "Only you can see this" message - """ - await ctx.respond("what is your favrouite color?",view=DropdownView(),ephemeral=True) - """ - - @whatcolor.error - async def color_error(self, ctx: Context, error): - return await ctx.respond( - error, ephemeral=True - ) # ephemeral makes "Only you can see this" message - - -def setup(bot): - bot.add_cog(DropdownExample(bot)) diff --git a/examples/interactionsBot/cogs/slashGroup_cog.py b/examples/interactionsBot/cogs/slashGroup_cog.py deleted file mode 100644 index b42076bb0c..0000000000 --- a/examples/interactionsBot/cogs/slashGroup_cog.py +++ /dev/null @@ -1,56 +0,0 @@ -import discord -from discord.commands.commands import Option, SlashCommandGroup -from discord.ext import commands -from discord.ext.commands.context import Context - - -class SlashGroupExample(commands.Cog): - def __init__(self, bot): - self.bot = bot - - # - moderation = SlashCommandGroup("moderation", "Commands related to moderation.") - - @moderation.command(guild_ids=[...], description="kick some people") - async def kick( - self, - ctx: Context, - member: Option(discord.Member), - reason: Option(str, description="reason"), - ): - - # check kick members permission for the author - if ctx.author.guild_permissions.kick_members == True: - # https://docs.pycord.dev/en/master/api.html#discord.Member.kick - await member.kick(reason=reason) - await ctx.respond("hello") - else: - - await ctx.respond( - "you dont have the permission to do so!", ephemeral=True - ) - - @moderation.command(guild_ids=[...], description="ban some people") - async def ban( - self, - ctx: Context, - member: Option(discord.Member), - reason: Option(str, description="reason"), - ): - - # check ban members permission for the author - if ctx.author.guild_permissions.ban_members == True: - # https://docs.pycord.dev/en/master/api.html#discord.Member.ban - await member.ban(reason=reason) - await ctx.respond("done") - else: - await ctx.respond( - "you dont have the permission to do so!", ephemeral=True - ) - - # Adds the application command - bot.add_application_command(moderation) - - -def setup(bot): - bot.add_cog(SlashGroupExample(bot)) diff --git a/examples/interactionsBot/cogs/slashOption_cog.py b/examples/interactionsBot/cogs/slashOption_cog.py deleted file mode 100644 index b793fc8229..0000000000 --- a/examples/interactionsBot/cogs/slashOption_cog.py +++ /dev/null @@ -1,39 +0,0 @@ -import discord -from discord.commands import Option, slash_command -from discord.ext import commands -from discord.ext.commands.context import Context - - -class SlashOptionExample(commands.Cog): - def __init__(self, bot): - self.bot = bot - - # slash commands with options - @slash_command(guild_ids=[...], name="avatar", description="check someones avatar!") - async def av( - self, - ctx, - # - member: Option(discord.Member, description="the user you want the avatar of."), - ): - """ - ephemeral makes "Only you can see this" message - - `await ctx.respond(embed=discord.Embed().set_image(url=str(member.avatar.url)),ephemeral=True)` - - embed docs - https://docs.pycord.dev/en/master/api.html#embed - member docs - https://docs.pycord.dev/en/master/api.html#discord.Member - """ - return await ctx.respond( - embed=discord.Embed().set_image(url=str(member.avatar.url)) - ) - - @av.error - async def av_error(self, ctx: Context, error): - return await ctx.respond( - error, ephemeral=True - ) # ephemeral makes "Only you can see this" message - - -def setup(bot): - bot.add_cog(SlashOptionExample(bot))