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()
Related
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.
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
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 recently a discord bot for small server with friends. It is designed to answer when mentioned, depending on user asking. But the problem is, when someone mention bot from a phone app, bot is just not responding. What could be the problem?
Code:
import discord
from discord.ext import commands
from discord.ext.commands import Bot
import asyncio
bot = commands.Bot(command_prefix = '=')
reaction = "🤡"
#bot.event
async def on_ready():
print('Bot is ready.')
#bot.listen()
async def on_message(message):
if str(message.author) in ["USER#ID"]:
await message.add_reaction(emoji=reaction)
#bot.listen()
async def on_message(message):
mention = f'<#!{BOT-DEV-ID}>'
if mention in message.content:
if str(message.author) in ["user1#id"]:
await message.channel.send("Answer1")
else:
await message.channel.send("Answer2")
bot.run("TOKEN")
One thing to keep in mind is that if you have multiple functions with the same name, it will only ever call on the last one. In your case, you have two on_message functions. The use of listeners is right, you just need to tell it what to listen for, and call the function something else. As your code is now, it would never add "🤡" since that function is defined first and overwritten when bot reaches the 2nd on_message function.
The message object contains a lot of information that we can use. Link to docs
message.mentions gives a list of all users that have been mentioned in the message.
#bot.listen("on_message") # Call the function something else, but make it listen to "on_message" events
async def function1(message):
reaction = "🤡"
if str(message.author.id) in ["user_id"]:
await message.add_reaction(emoji=reaction)
#bot.listen("on_message")
async def function2(message):
if bot.user in message.mentions: # Checks if bot is in list of mentioned users
if str(message.author.id) in ["user_id"]: # message.author != message.author.id
await message.channel.send("Answer1")
else:
await message.channel.send("Answer2")
If you don't want the bot to react if multiple users are mentioned, you can add this first:
if len(message.mentions)==1:
A good tip during debugging is to use print() So that you can see in the terminal what your bot is actually working with.
if you print(message.author) you will see username#discriminator, not user_id
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)))