| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- from discord.ext import commands
- import discord
- import time
- from typing import Optional
- from query.channel import get_interact, set_interact, set_games
- from query.user import ignore_user, unignore_user, is_ignored
- from common.logging import report
- from common.settings import check_ignore
- class MyView(discord.ui.View):
- #@discord.ui.button(label="Duplicate the current settings to all channels", row=0, style=discord.ButtonStyle.primary)
- #async def first_button_callback(self, button, interaction):
- # await interaction.response.send_message("You pressed me!")
- #@discord.ui.button(label="Reset all settings to default", row=0, style=discord.ButtonStyle.primary)
- #async def second_button_callback(self, button, interaction):
- # await interaction.response.send_message("You pressed me!")
- @discord.ui.select(
- row=0,
- options=[
- discord.SelectOption(
- label="Interact",
- description="Have me interact in the chat."
- ),
- discord.SelectOption(
- label="Ignore",
- description="Do not mingle in the chat."
- ),
- ]
- )
- async def first_select_callback(self, select, interaction):
- await set_interact(self.bot.pg, select.values[0], interaction.channel.id)
- @discord.ui.select(
- row=1,
- options=[
- discord.SelectOption(
- label="Games",
- description="Activate discord games like ActiveRPG."
- ),
- discord.SelectOption(
- label="Serious",
- description="No fun commands."
- ),
- ]
- )
- async def second_select_callback(self, select, interaction):
- await interaction.response.send_message(f"Awesome! I like {select.values[0]} too!")
- class ChannelSettingsModal(discord.ui.Modal):
- def __init__(self, *args, **kwargs) -> None:
- super().__init__(*args, **kwargs)
- self.add_item(discord.ui.InputText(label="Interact"))
- self.add_item(discord.ui.InputText(label="Games", style=discord.InputTextStyle.long))
- async def callback(self, interaction: discord.Interaction):
- print(f"interaction: {interaction}")
- embed = discord.Embed(title="Test Modal Results")
- embed.add_field(name="Interact", value=self.children[0].value)
- embed.add_field(name="Games", value=self.children[1].value)
- await interaction.response.send_message(embeds=[embed])
- class Admin(commands.Cog):
- def __init__(self, bot): # Special method that is called when the cog is loaded
- self.bot = bot
- @commands.slash_command()
- async def flavor(self, ctx):
- await ctx.respond("Adjust channel settings.", view=MyView())
- @commands.slash_command()
- async def modal_slash(self, ctx: discord.ApplicationContext):
- """Shows an example of a modal dialog being invoked from a slash command."""
- modal = ChannelSettingsModal(title="Adjust channel functions.")
- await ctx.send_modal(modal)
- @commands.slash_command(
- description="Get the bot's current websocket and API latency.",
- brief="Test latency",
- help="Test latency by polling the gateway and API."
- )
- @commands.has_permissions(administrator=True)
- async def ping(self, ctx: commands.Context):
- # Halt on ignore list.
- if await check_ignore(self.bot.pg, ctx.author):
- return
- start_time = time.time()
- await ctx.respond(f"Pong!\nGateway heartbeat in {round(self.bot.latency * 1000)}ms.")
- end_time = time.time()
- await ctx.send(f"API roundtrip latency {round((end_time - start_time) * 1000)}ms.")
- @commands.slash_command(
- description="Send a message",
- brief="Chat",
- help="Make a chat message",
- aliases= ("say", "pm", "dm", "echo", "print")
- )
- #async def msg(self, ctx: commands.Context, channel: Optional[discord.TextChannel], user: Optional[discord.User], *, message: str = None):
- async def msg(self, ctx: commands.Context, channel: Optional[discord.TextChannel], user: Optional[discord.User], message: str = None):
- # Halt on ignore list.
- if await check_ignore(self.bot.pg, ctx.author):
- return
- #print(f"Is Guild Admin: {ctx.author.guild_permissions.administrator}")
- #print(f"Author has perms in main output channel: {self.bot.get_channel(self.bot.OUTPUT_CHANNEL_ID).permissions_for(ctx.author).send_messages}")
- #print(f"Has author perms in main output channel: {ctx.author.permissions_in(self.bot.get_channel(self.bot.OUTPUT_CHANNEL_ID)).send_messages}")
- if not message:
- if channel:
- await ctx.respond(f"What would you like me to say in `{channel}`?")
- elif user:
- await ctx.respond(f"What would you like me to say to `{user}`?")
- else:
- await ctx.respond("What would you like me to say?")
- elif channel:
- if await get_interact(self.bot.pg, channel.id) or channel.permissions_for(ctx.author).administrator or self.bot.get_channel(self.bot.OUTPUT_CHANNEL_ID).permissions_for(ctx.author).send_messages:
- await channel.send(message)
- await ctx.respond("Message sent.")
- await report(self.bot, f"`{ctx.author}` @ {channel.mention}: {message}", ctx.guild)
- else:
- await ctx.respond(f"Interactive mode for {channel} is deactivated.")
- elif user and self.bot.get_channel(self.bot.OUTPUT_CHANNEL_ID).permissions_for(ctx.author).send_messages:
- await user.send(message)
- await ctx.respond("Message sent.")
- await report(self.bot, f"`{ctx.author}` @ `{user.name}`: {message}")
- else:
- await ctx.respond(message)
- await report(self.bot, f"`{ctx.author}` has sent {message} locally.", ctx.guild)
- @commands.slash_command(
- description="Change status.",
- brief="Set status",
- help="Update the bot's status."
- )
- async def status(self, ctx: commands.Context, *, text: str):
- # Halt on ignore list.
- if await check_ignore(self.bot.pg, ctx.author):
- return
- await self.bot.change_presence(activity=discord.Game(name=text))
- await report(self.bot, f"`{ctx.author}` has set my status to `{text}`.")
- @commands.slash_command(
- description="Get ignored.",
- brief="Ignore sender",
- help="Will have the bot ignore the user from now on."
- )
- async def ignoreme(self, ctx: commands.Context):
- # Halt on ignore list.
- if await check_ignore(self.bot.pg, ctx.author):
- return
- await ignore_user(self.bot.pg, ctx.author.id)
- await ctx.respond("To revert this use the `/unignoreme` command.")
- await report(self.bot, f"`{ctx.author}` has requested to be ignored.")
- @commands.slash_command(
- description="No longer get ingored.",
- brief="Un-ignore sender",
- help="No longer will the bot ignore the user."
- )
- async def unignoreme(self, ctx: commands.Context):
- await unignore_user(self.bot.pg, ctx.author.id)
- await ctx.respond(f"I shall now interact with you again where my channel settings allow it.")
- await report(self.bot, f"`{ctx.author}` has requested to be un-ignored.")
- @commands.slash_command(
- description="Ignore status for user.",
- brief="Check if user is ingored",
- help="Verify if the user is being ignored."
- )
- async def isignored(self, ctx: commands.Context, user: Optional[discord.User]):
- # Halt on ignore list.
- if await check_ignore(self.bot.pg, ctx.author):
- return
- if not user:
- user = ctx.author
- if await is_ignored(self.bot.pg, user.id):
- await ctx.respond(f"I am ingoring `{user}`.")
- else:
- await ctx.respond(f"I am not ignoring `{user}`.")
- def setup(bot): # Called by Pycord to setup the cog
- bot.add_cog(Admin(bot)) # Add the cog to the bot
|