I was trying to create come channels when bot starts. I want them to create in function on_ready. But await guild.create_text_channel("Channel") requires guild by itself. Usually when I want to create it by command I make something like this:
#client.command()
async def create(ctx):
guild = ctx.guild
await guild.create_text_channel(name="test channel")```
But I need ctx to create guild. So the question is: How do I create a channel without ctx?
You simply need to get a discord.Guild instance, you can get that using Bot.get_guild
async def on_ready():
await client.wait_until_ready()
guild = client.get_guild(id_here)
await guild.create_text_channel(name="whatever")
If you want to create a channel in all the guilds the bot is in you can loop through client.guilds
async def on_ready():
await client.wait_until_ready()
for guild in client.guilds:
await guild.create_text_channel(name="whatever")
Reference:
Bot.get_guild
Bot.guilds
Ran into the same problem with disnake, and it took me reading through the api and testing things to figure it out. Since disnake is supposed to be forked off of discord.py try this
#commands.command(name="obvious", description="given")
async def command(ctx: Context):
await ctx.message.guild.create_text_channel("channel_name")
This solved my issue, and now I just need to add the parameters to properly sort channel creation.
Related
I made two working bot commands, one that get id's from users connected to a voice channel and the other one move single members to another channel.
How can i automate the process? I need to move every member without typing the username on discord chat
Thanks in advance
#bot.command()
async def move(ctx, channel : discord.VoiceChannel, *members: discord.Member):
for member in members:
await member.move_to(channel)
#bot.command()
async def people(ctx):
channel = bot.get_channel(ID_HERE)
member_ids = list(channel.voice_states.keys())
return member_ids
#*members should get member_ids
You can just create another command from which you call the other two functions:
#bot.command()
async def moveAll(ctx, channel:discord.VoiceChannel):
members_connected = await people(ctx) #Get the people
for member_id in people_connected:
member = await ctx.guild.fetch_member(member_id)
await member.move_to(channel)
I made a small discord bot, and I need him to delete a message from the writer and then post something. When I use #bot.event it works fine but it's the only thing working in code(only the delete part works). And how to use ctx.message.delete?
#bot.command(aliases=["IDC"])
async def idc(ctx):
ctx.channel.send("https://cdn.discordapp.com/attachments/586526210469789717/838335535944564756/Editor40.mp4")
You just need to save the channel for later use.
#bot.command(aliases=["IDC"])
async def idc(ctx):
chn = ctx.channel
await ctx.message.delete()
await chn.send("https://cdn.discordapp.com/attachments/586526210469789717/838335535944564756/Editor40.mp4")
Your bot will need the delete messages permission.
EDIT:
This will also work, but it is recommended to use the first way as it can be faster.
#bot.command(aliases=["IDC"])
async def idc(ctx):
await ctx.send("https://cdn.discordapp.com/attachments/586526210469789717/838335535944564756/Editor40.mp4")
await ctx.message.delete()
So, I'm new to programming and I have a discord bot, I used client = discord.Client() instead of using client = commands.Bot(...), and now i want to be able to kick users. But I can't see any command using my client to kick someone using their username.I konw it's simpler to do it with a bot but i have already such a big code that I don't want to redo it.
Help would be appreciated :)
If you change it to commands.Bot everything is going to work just fine (if you add await client.process_commands(message) at the end of the on_message event. But nevertheless here's how to make a kick 'command' using the on_message event:
#client.event
async def on_message(message):
guild = message.guild
if message.startswith('!kick'):
args = message.content.split()[1:]
member_id = int(args[0])
member = guild.get_member(member_id)
reason = ' '.join(args[1:])
await member.kick(reason=reason)
# If you actually change your mind and want to use `commands.Bot`
await client.process_commands(message)
# To invoke
# !kick 172399871237987 drama seeker
I strongly DO NOT recommend using this method at all, just compare how much better, easier and cleaner is with commands
#client.command()
#commands.has_permissions(kick_members=True)
async def kick(ctx, member: discord.Member, *, reason):
await member.kick(reason=reason)
# While invoking here you can actually mention the user
# !kick #some_user drama seeker
# But you also can use the ID
# !kick 761876328716327186 drama seeker
It's only 4 lines, 3 if we don't count the has_permissions decorator.
I made a working AFK code, but i need it to slice (delete) '[AFK]' if the user sends a message. The code is:
#client.command()
async def afk(ctx, *, message="Dind't specify a message."):
global afk_dict
if ctx.author in afk_dict:
afkdict.pop(ctx.author)
await ctx.send('Welcome back! You are no longer afk.')
await ctx.author.edit(nick=ctx.author.display_name(slice[-6]))
else:
afk_dict[ctx.author] = message
await ctx.author.edit(nick=f'[AFK] {ctx.author.display_name}')
await ctx.send(f"{ctx.author.mention}, You're now AFK.")
I got no errors. i only need await ctx.author.edit(nick=ctx.author.display_name(slice[-6])) to be working and i'll be happy.
You need to have a on_message event. Something like this should work.
#client.event
async def on_message(message):
if message.author in afk_dict.keys():
afk_dict.pop(message.author.id, None)
nick = message.author.display_name.split('[AFK]')[1].strip()
await message.author.edit(nick=nick)
I also recomend you to store user id's instead of usernames.
To do that you just have to use member.id instead of member.
I am trying to code a command into my Discord Bot that when triggered will actively delete new messages from a specific user.
I have tried a couple variations of using ctx but I honestly I don't entirely understand how I could use it to accomplish this.
This block of code deletes the message that triggers it. Because of this, I think I am taking the wrong approach because it only deletes from whoever triggers it and only if they type the command. Obviously, I am new to this. I would appreciate any and all help. Thanks so much :)
#client.event
async def on_message(ctx):
message_author = ctx.author.id
if message_author == XXXXXXXXXXXXXXXXX:
await ctx.message.delete()
There is no more CTX in discord rewrite (DiscordPy 1.0+) instead you should do:
#client.event
async def on_message(message):
if message.author.id == XXXXXXXXXXXXXXXXX:
await message.delete()
Source: link
You can make a command to add the users to a list then use on_message to check if a user is in that list and then delete it if true
dels=[]
#bot.event
async def on_message(msg):
if msg.author.id in dels:
await bot.delete_message(msg)
await bot.process_commands(msg)
#bot.command(name='del_msg')
async def delete_message(con,*users:discord.Member):
for i in users:
dels.append(i.id)
await con.send("Users {} have been added to delete messages list".format(" ,".join(users)))