I would like to have my bot edit a message if it detects a keyword, i'm not sure how to edit the message though.
I've looked through the documentation but can't seem to figure it out. I'm using discord.py with python 3.6.
This is the code:
#bot.event
async def on_message(message):
if 'test' in message.content:
await edit(message, "testtest")
This is the error:
File "testthing.py", line 67, in on_message
await edit(message, "test")
NameError: name 'edit' is not defined
I would like the bot to edit a message to "testtest" if the message contains the word test, but i just get an error.
You can use the Message.edit coroutine. The arguments must be passed as keyword arguments content, embed, or delete_after. You may only edit messages that you have sent.
await message.edit(content="newcontent")
Here's a solution that worked for me.
#client.command()
async def test(ctx):
message = await ctx.send("hello")
await asyncio.sleep(1)
await message.edit(content="newcontent")
Did you do this:
from discord import edit
or this:
from discord import *
before using message.edit function?
If you did it, maybe the problem is with your discord.py version.
Try this:
print(discord.__version__)
If you are wanting to update responses in discord.py you have to use:
#tree.command(name = 'foobar', description = 'Send the word foo and update it to say bar')
async def self(interaction: discord.Interaction):
await interaction.response.send_message(f'foo', ephemeral = True)
time.sleep(1)
await interaction.edit_original_response(content=f'bar')
Assign the original message to a variable. Reference the variable with .edit(content='content').
(You need "content=" in there).
#bot.command()
async def test(ctx):
msg = await ctx.send('test')
await msg.edit(content='this message has been edited')
#bot.event
async def on_message(message):
if message.content == 'test':
messages=await message.channel.send("CONTENT")
await asyncio.sleep(INT)
await messages.edit(content="NEW CONTENT")
Please try to add def to your code like this:
#bot.event
async def on_message(message):
if 'test' in message.content:
await edit(message, "edited !")
This is what I did:
#bot.event
async def on_message(message):
if message.content == 'test':
await message.channel.send('Hello World!')
await message.edit(content='testtest')
I don't know if this will work for you, but try and see.
Related
I am making a discord chat bot. I want to set command "How are You?" But my program only choose 'How' . I need some suggestions.TIA
import discord
from discord.ext import commands
client = commands.Bot(command_prefix= '=')
#client.event
async def on_ready():
print('Bot is online')
#client.command()
async def hello(ctx):
await ctx.send('hello')
#client.command()
async def how_are_you(ctx):
await ctx.send('I am good')
client.run('token')
If you want to use a command, i don't think you can register one with spaces in it, however there are a couple of workarounds:
First you can use the first word of your string as the command name and then check for the content of the message:
#client.command()
async def how(ctx):
if ctx.message.content.lower() == "=how are you":
await ctx.send("I'm fine thanks")
else:
pass
The = prefix is needed in the message, replace it when you want to change your prefix.
Or, the second option would be to set up an on_message listener.
#client.event
async def on_message(message):
if message.content.lower().startswith("=how are you"):
await message.channel.send("I'm fine thanks.")
await client.process_commands(message)
import discord
from discord.ext import commands
client = commands.Bot(command_prefix='sk?')
token = ""
#client.event
async def on_message():
if 'WORD' in message.content:
await member.ban(reason = WORD)
client.run(token)
I get the error "takes 0 positional arguments but 1 was given" and I don't know what is wrong with my code.
I tried adding on_message(member): and member = message.author() above if 'WORD' in message.content:
I have no idea what to do or what I did wrong.
It also says that for all messages which is really annoying.
on_message takes the message argument, also member should be message.author
#client.event
async def on_message(message):
if "WORD" in message.content:
await message.author.ban(reason="WORD")
# Remember to add `process_commands`
await client.process_commands(message)
You should also add process_commands at the end of the event so commands work
I want to to make some code that will mention the name of the user that sends the message, this is what I have tried:
Im new to making discord bots, and python so any help would be perfect
#client.command()
async def hello(ctx, member):
await ctx.send(f"hello, {member}")
You can do this with user.name
#client.command()
async def hello(ctx):
await ctx.send(f"hello, {ctx.author.name}")
The above can be called by {prefix}hello, if you want to say "hello, {name}" even when user just send "hello" (without prefix), then use on_message event.
#client.event
async def on_message(message):
if message.author.bot: return
if message.content.lower() == "hello":
await message.channel.send(f"Hello, {message.author.name}")
await client.process_commands(message)
Docs: on_message, user.name
Assuming you want to mention the user who used the command. the ctx argument has a lot of attributes one of them is the author
#client.command()
async def hello(ctx, member):
await ctx.send(f"hello, {ctx.author.mention}")
If you want the user to say hello to another user, he can mention or just type the name.
#client.command()
async def hello(ctx, *, user: discord.Member = None):
if user:
await ctx.send(f"hello, {user.mention}")
else:
await ctx.send('You have to say who do you want to say hello to')
All of the above providing you are using discord.ext.commands
This should work:
str(ctx.message.author)
Or:
str(ctx.author)
I am trying to make a fun bot for just me and my friends. I want to have a command that says what the authors username is, with or without the tag. I tried looking up how to do this, but none worked with the way my code is currently set up.
import discord
client = discord.Client()
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$WhoAmI'):
##gets author.
await message.channel.send('You are', username)
client.run('token')
I hope this makes sense, all of the code i have seen is using the ctx or #client.command
The following works on discord.py v1.3.3
message.channel.send isn't like print, it doesn't accept multiple arguments and create a string from it. Use str.format to create one string and send that back to the channel.
import discord
client = discord.Client()
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$WhoAmI'):
await message.channel.send('You are {}'.format(message.author.name))
client.run('token')
Or you can just:
import discord
from discord import commands
client = commands.Bot(case_insensitive=True, command_prefix='$')
#client.command()
async def whoAmI(ctx):
await ctx.send(f'You are {ctx.message.author}')
If you want to ping the user:
import discord
client = discord.Client()
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$WhoAmI'):
await message.channel.send('You are', message.author.mention)
client.run('token')
Here is the sendMessage function:
async def sendMessage(color, title, value, should_delete=True, channel=""):
embed = discord.Embed(color=color)
embed.add_field(name=title, value=value, inline=False)
if channel == "":
msg = await client.send_message(message_obj.channel, embed=embed)
else:
msg = await client.send_message(client.get_channel(channel), embed=embed)
if should_delete:
await delete_msg(msg)
The bot can mention anyone but everyone and here. Despite having mention everyone permission.
sendMessage(OK_COLOR_HASH, "title", "Hello #everyone")
Edit: When I converted the message type to the normal one instead of embed one, it worked.
You can try to sending a mention to everyone through the default_role attribute
#bot.command(pass_context=True)
async def evy(msg):
await bot.say(msg.message.server.default_role)
You can try this block code. roles return a list all guild's roles, but first role always a default guild role (default_role) and that's why you must use slicing function.
#bot.command()
async def test(ctx):
await ctx.send(ctx.message.guild.roles[0])
Or you can do something like this.
#bot.command()
async def test(ctx):
await ctx.send(ctx.message.guild.default_role)