making a thread using discord.py - python

i want my bot to make a thread after someones used a suggestion command so people can discuss it.
this is what i've got. i got no errors but it doesn't create a thread, everything else works.
#bot.tree.command(name="suggestion", description="Sends a suggestion to our suggestion channel!")
async def _suggestion(interaction, suggestion:str):
channel = bot.get_channel(1059976520376012811)
suggestEmbed = discord.Embed(color=0xFF5349)
suggestEmbed.set_author(name=f'New suggestion by {interaction.user}', icon_url = f'{interaction.user.avatar}')
suggestEmbed.add_field(name='The suggestion is:', value=f'{suggestion}')
message = await channel.send(embed=suggestEmbed)
await message.add_reaction(':white_check_mark:')
await message.add_reaction(':x:')
channel = bot.get_channel(int(1059976520376012811))
thread = await channel.create_thread(
name=f"{interaction.user}'s Suggestion Discussion")
await thread.send(f"Discuss {interaction.user}'s suggestion!")
embed=discord.Embed(title="Successful!", color=0x23B425)
embed.add_field(name="This command was successful.", value="Your suggestion was successfully posted in #suggestions", inline=False)
await interaction.response.send_message(embed=embed)```

#bot.tree.command(name="suggestion", description="Sends a suggestion to our suggestion channel!")
async def _suggestion(interaction, suggestion:str):
channel = bot.get_channel(1059976520376012811)
suggestEmbed = discord.Embed(color=0xFF5349)
suggestEmbed.set_author(name=f'New suggestion by {interaction.user}', icon_url = f'{interaction.user.avatar}')
suggestEmbed.add_field(name='The suggestion is:', value=f'{suggestion}')
message = await channel.send(embed=suggestEmbed)
await message.add_reaction('✅')
await message.add_reaction('❌')
channel = bot.get_channel(int(1059976520376012811))
thread = await message.create_thread(
name=f"{suggestion} Discussion",
reason="Suggestion")
embed=discord.Embed(title=f"Discuss {interaction.user}'s suggestion!", color=0xFF5349)
embed.add_field(name="Like, dislike or want to add onto someones suggestion?", value="Do that here! In this thread, you can discuss what you like, dislike or something smiliar you'd like to see!", inline=False)
embed.set_author(name = f'{interaction.user}', icon_url = interaction.user.avatar)
await thread.send(embed=embed)
embed=discord.Embed(title="Successful!", color=0x23B425)
embed.add_field(name="This command was successful.", value="Your suggestion was successfully posted in <#1059976520376012811>", inline=False)
await interaction.response.send_message(embed=embed)```

Ah, I see! You aspireate to craft a Discord bot that generates a suggestion followed by the creation of a discussion thread surrounding the suggestion. To bring this vision to life, it's crucial to ensure the create_thread method is accurately executed and accessible through the Discord API.
However, in the code snippet you've proffered, there appears to be an attempt to summon the create_thread method on the channel object. Regrettably, the Discord API does not possess the create_thread method as of my discord quitting in 2022. Nevertheless, all is not lost. Instead, by utilizing the create_webhook method to generate a new webhook, followed by the application of the send method on that webhook, a message can be posted within a new thread.
Here's an updated version of the code to reflect this approach:
#bot.command(name="suggestion", description="Sends a suggestion to our suggestion channel!")
async def suggestion(interaction, suggestion:str):
channel = bot.get_channel(1059976520376012811)
suggestEmbed = discord.Embed(color=0xFF5349)
suggestEmbed.set_author(name=f'New suggestion by {interaction.user}', icon_url = f'{interaction.user.avatar}')
suggestEmbed.add_field(name='The suggestion is:', value=f'{suggestion}')
message = await channel.send(embed=suggestEmbed)
await message.add_reaction('✅')
await message.add_reaction('❌')
webhook = await channel.create_webhook(name=f"{suggestion} Discussion")
await webhook.send("This is an example message")
embed=discord.Embed(title="Successful!", color=0x23B425)
embed.add_field(name="This command was successful.", value="Your suggestion was successfully posted in <#1059976520376012811>", inline=False)
await interaction.response.send_message(embed=embed)

Related

How do i code an afk command that comes out the way dyno runs

I need help making a afk command for my discord server. When the afk command is triggered, my bot doesn't respond with a reasoning when you ping the person whos afk. Also, when you return from being afk and type, the bot doesn't send a message saying "(user) is no longer afk". Please help me and tell me what i'm doing wrong and how can I fix this?
afkdict = {User: "their reason"} # somewhere in the code
#bot.command("afk")
async def afk(ctx, reason=None):
afkdict[ctx.user] = reason
await ctx.send("You are now afk. Beware of the real world!")
#bot.event
async def on_message(message):
afkdict = {user: "their reason"}
# some other checks here
for user, reason in afkdict.items():
if user in message.mentions:
if reason is None:
reason = ""
embed = discord.Embed(title=f"{user} is AFK", color=0xFF0000, description=reason[:2500])
await message.reply()
I was expecting this to work, the way dyno works. When i ran the command i got a message back saying user has no context. I dont know what to do anymore.
I think there's a couple of issues. Firstly, you are redefining afkdict in your on_message function it doesn't matter that you're adding users to it in the afk command. Secondly, when you're doing await message.reply(), you're not actually sending the created embed along with it.
I've resolved those problems and changed the logic slightly. Instead of iterating over the users in the afk_dict and checking if they're mentioned, we're iterating over the mentions and seeing if they're in the afk_dict. I'm also using user.id rather user objects as keys.
# defined somewhere
afk_dict = {}
#bot.command()
async def afk(ctx, reason=None):
afk_dict[ctx.user.id] = reason
await ctx.send("You are now afk. Beware of the real world!")
#bot.event
async def on_message(message):
# do whatever else you're doing here
for user in message.mentions:
if user.id not in afk_dict:
continue
# mentioned used is "afk"
reason = afk_dict[user.id] or ""
embed = discord.Embed(title=f"{user.mention} is AFK", color=0xFF0000, description=reason[:2500])
await message.reply(embed=embed)
It looks like you are missing some pieces in your code. Here is an updated version of the code:
afkdict = {}
#bot.command("afk")
async def afk(ctx, reason=None):
user = ctx.message.author
afkdict[user] = reason
await ctx.send(f"You are now AFK. {'Reason: ' + reason if reason else ''}")
#bot.event
async def on_message(message):
for user, reason in afkdict.items():
if user in message.mentions:
if reason is None:
reason = ""
embed = discord.Embed(title=f"{user} is AFK", color=0xFF0000, description=reason[:2500])
await message.channel.send(embed=embed)
if message.author in afkdict:
afkdict.pop(message.author)
await message.channel.send(f"{message.author} is no longer AFK")
In this code, the afk command will add the user who runs the command to the afkdict dictionary along with the reason for being AFK. The on_message event handler will then check if any of the mentioned users are in the afkdict and if so, it will send an embed with the AFK status and reason. Finally, if the author of the message is in the afkdict, it will remove them from the dictionary and send a message indicating that they are no longer AFK.

Message Will Not Send To Channel w/ Discord.py

I am programming a discord bot that will send an embed to a separate channel when someone uses a command to prevent abuse and so our admins can keep an eye on usage.
I have this code:
#client.command()
#has_permissions(manage_messages=True)
async def purge(ctx, amount=5):
await ctx.channel.purge(limit = amount + 1) # Plus one is so that it also deletes the players purge message
embed = discord.Embed(title="Purged Channel", description="", colour=0xe74c3c, timestamp=datetime.utcnow())
embed.set_author(name="CS Moderation", icon_url="https://cdn.discordapp.com/attachments/938857720268865546/938863881726615602/ca6.png")
embed.add_field(name="Staff Member", value=ctx.author.mention, inline=False)
embed.set_footer(text="Made by HeadOfTheBacons#6666", icon_url=None)
channel = client.get_channel("938898514656784404") #Change ID of logging channel if needed
await channel.send(embed = embed)
When I run this, I cannot get the embed to be sent to a different channel. I have tried checking other posts already but I have not had much luck at all. I have no errors when I use the command or try to run the bot. The point here is when someone uses the command to make a new embed, set up the preferences, specify the specific channel ID where I want this embed to be sent to, and have the computer send it off to that channel. However, it is not sending. Why is my code not working? What do I need to change in my code to make this send a message?
Check if you have an on_command_error function in your program, it could be the reason of your no error problem.
Also not that the embed.footer can't be None, and the client.get_channel requires an Integer.
So you can try out the following code, it should work:
from datetime import datetime
import discord
from discord.ext import commands
from discord.ext.commands import has_permissions
client = commands.Bot(command_prefix="prefix")
#client.command()
#has_permissions(manage_messages=True)
async def purge(ctx, amount=5):
await ctx.channel.purge(limit=amount + 1) # Plus one is so that it also deletes the players purge message
embed = discord.Embed(title="Purged Channel", description="", colour=0xe74c3c, timestamp=datetime.utcnow())
embed.set_author(name="CS Moderation", icon_url=client.user.avatar_url)
embed.add_field(name="Staff Member", value=ctx.author.mention, inline=False)
embed.set_footer(text="Made by HeadOfTheBacons#6666")
channel = client.get_channel(938898514656784404) # Change ID of logging channel if needed
await channel.send(embed=embed)
client.run("TOKEN")

Suggest command not working in discord.py

I'm currently coding a bot in discord.py that does mod commands like kick, etc. I'm trying to make a command that you can suggest something to the server like they make a channel called suggestion-channel, when they do the command it sends it to that channel. I have a command that sends a message to a specific channel for bot suggestions, but that's the closest I've gotten. below is the code that I have for the suggest command in cogs, yes I use command handling.
#commands.command()
async def suggest(self ,ctx , *, suggestion):
embed = discord.Embed(title=f"Suggestion from {ctx.author.name}", description=suggestion, color=discord.Color.blue())
embed.set_footer(text=f"Suggestion created by {ctx.author}")
channel = discord.utils.get(message.server.channels, name="suggestion-channel")
message = await channel.send(embed=embed)
await message.add_reaction("✅")
await message.add_reaction("❌")
await ctx.message.delete()
await ctx.send("Thank you for your suggestion!")
First of all, you're not getting the channel in an on_message event, or using a user's message to get the server's channels. You'll have to replace message with ctx. Secondly, ctx.server is no longer valid since discord.py's migration. It would now be known as ctx.guild, which you can read about in their docs.
channel = discord.utils.get(message.server.channels, name="suggestion-channel")
# change this to:
channel = discord.utils.get(ctx.guild.channels, name="suggestion-channel")
Otherwise, if you find that any of your other commands are not working, you may have not processed your on_message event. You can view questions on how to solve this:
Why does on_message stop commands from working?
await process_commands(message) [docs]
discord.py #bot.command() not running

Discord.py Adding Reactions For Poll

I've got this code for my bot, I'm trying to make a !poll command. In theory, I'll type !poll (question), and then the bot will send my questions as an embed, and under that embed, add a thumbsup and thumbsdown reaction so people can vote. However, I just cannot work out how to make it add the reaction, no matter how hard I try and research. Here's the code:
#commands.has_permissions(administrator=True)
async def poll (ctx, *, message):
embed=discord.Embed(title="New Poll!", description = message, color=0xff0000)
await ctx.send(embed=embed)
So what do I now need to add to my code so it can add a 👍 and 👎 reaction to the embed? Thanks.
Messageable.send returns a discord.Message instance, you then simply .add_reaction()
message = await ctx.send(embed=embed)
await message.add_reaction('👍')
await message.add_reaction('👎')
Reference:
Messageable.send
Message.add_reaction

How do I make the bot disconnect the voice channel with a command like -disconnect in lavalink.py? {CLOSED FOUND ALTERNATIVE METHOD}

I'm trying to make my discord bot disconnect the voice channel with a command like -disconnect using Lavalink. I have tried doing it in various approaches but it never seems to work for me. I also can't find many examples online. The most recent thing i've tried is this:
#commands.command()
async def leave(self, ctx):
guild_id = int(event.player.guild_id)
await self.connect_to(guild_id, None)
Tell me if you know how to make a disconnect command. Thanks!!!
This is discord.py rewrite btw.
To make a disconnect command is lavalink all you have to do is:
await self.connect_to(ctx.guild.id, None)
Here's an example:
import asyncio
#client.command()
async def disconnect(ctx):
channel = client.get_channel(channelIdHere)
vc = await channel.connect()
await asyncio.sleep(2) # Waits 2 seconds before leaving the vc
await vc.disconnect()
You should check the documentation for more information.

Categories