I'm making a bot for discord and I want to have a custom help-message. I tried:
from discord.ext import commands
bot = commands.Bot(command_prefix="!")
#bot.command()
async def help(ctx):
member = ctx.author
await ctx.send(member, "test successful")
bot.run('TOKEN')
It's supposed to send a private message to the user and send them whatever. But when I enter !help the bot doesn't even react to the message.
Sending a message only has one positional argument (the message itself) and options. In order to send a DM message, you will need to send on the member class instead.
await member.send("test successful")
When defining your bot, the default is to have an automatic help command. commands.Bot has a keyword parameter help_command that you can set to None to make sure that the automatic help command is turned off. Also, to send a direct message to someone, you have to use user.send(). Your original command also required you to mention the user, as it took a Member as an argument. I'm not sure if that was what you want, but I wouldn't think so.
Here's what I would do:
bot = commands.Bot(command_prefix="!",help_command=None)
#bot.command()
async def help(ctx):
await ctx.author.send("test successful")
Edit: clarity in my explanation.
Related
I'm making a discord bot with discord.py and I want to a specific user when a user uses a specific command.
from discord import DMChannel
client = discord.Client()
client = commands.Bot(command_prefix=']')
#client.command(name='dmsend', pass_context=True)
async def dmsend(ctx, user_id):
user = await client.fetch_user("71123221123")
await DMChannel.send(user, "Put the message here")
When I give the command ]dmsend nothing happens. And I've tried dmsend also. But nothing happened.
A few things I noticed:
You defined client twice, that can only go wrong.
First) Remove client = discord.Client(), you don't need that anymore.
If you want to send a message to a specific user ID, you can't enclose it in quotes. Also, you should be careful with fetch, because then a request is sent to the API, and they are limited.
Second) Change await client.fetch_user("71123221123") to the following:
await client.get_user(71123221123) # No fetch
If you have the user you want the message to go to, you don't need to create another DMChannel.
Third) Make await DMChannel.send() into the following:
await user.send("YourMessageHere")
You may also need to enable the members Intent, here are some good posts on that:
https://discordpy.readthedocs.io/en/stable/intents.html
How do I get the discord.py intents to work?
A full code, after turning on Intents, could be:
intents = discord.Intents.all()
client = commands.Bot(command_prefix=']', intents=intents)
#client.command(name='dmsend', pass_context=True)
async def dmsend(ctx):
user = await client.get_user(71123221123)
await user.send("This is a test")
client.run("YourTokenHere")
Use await user.send("message")
EDIT: I have fixed the code by just using another method, thanks for the other helpful answers though!
I'm making a say command, it just takes the user's message and repeats it via the bot. But it can be abused to ping everyone etc. So I want to make it so when the User isn't an admin it checks the message for #everyone or #here. Then it says that you can't ping everyone and reacts to the output 'You cant ping everyone' message with a custom command.
It's also in a cog btw. The command, when used, doesn't throw any console errors, instead not doing anything.
EDIT: I have fixed the code by just using another method, thanks for the other helpful answers though!
#commands.command()
async def say(self, ctx, *, message):
await ctx.message.delete()
if not ctx.message.author.guild_permissions.administrator:
if "#everyone" in message or "#here" in message:
antiping = "You can't mention everyone through me!"
await ctx.send(antiping)
await antiping.add_reaction(emoji=':Canteveryone:890319257534103603')
return
else:
await ctx.message.delete()
await ctx.send(message)
You can use allowed_mentions for this. Two ways of using this:
Pass in Bot when initializing:
client = bot(.....,
allowed_mentions = discord.AllowedMentions(everyone = False)
)
You can also pass the same object when sending a message:
ctx.send(...,
allowed_mentions = discord.AllowedMentions(everyone = False)
)
I'd recommend you read the docs for more info on how it works.
You can adjust mentions for the following attributes:
everyone
replied_user
roles
users
A way of doing this would be when you execute your command, it modifies the role "everyone" so that they cant ping the role
Admins bypass ALL restrictions so they would still be able to ping the role besides
My python discord bot keeps getting errors when I tell it to send a message in a channel
#client.command()
async def log(ctx, msg):
channel = client.get_channel(852610465871036416)
await channel.message.send(msg)
PS: its fixed now
Try This:
#client.command()
async def log(ctx, msg):
await ctx.send(msg)
I'm Pretty Sure You Needed to Use ctx.send
Tortle.py is completely right, however, you can do many forms of the ctx.___("stuff") . The first and simplest example would be ctx.send("stuff") , this will send the "stuff" in the channel where the user called the function (the thing the bot is doing.)
You could also do ctx.author.send this will DM (direct message) the author that called the function.
You could also do ctx.reply this will reply to the message that the user sends.
I hope you found this useful and feel free to like on this message for any extra support!
in last line
await channel.message.send(msg)
?
try it
await channel.send(msg)
#client.command()
async def log(ctx, message):
await ctx.get_channel(852610465871036416).send(“your message”)
This would work
import discord
from discord.ext import commands
async def anyfunc(ctx):
await ctx.send(f"{ctx.message.author.mention}, "Hi!")
bot_message_id = ((bot's_last_message)).id
What to put in (()) to make the bot remember the id of the message that it will send?
Is there a way to take the id of the next (for example, user's) message (that hasn't been sent yet) as a variable?
ctx.send() returns the discord.Message instance of the message it sent, so you can store it in a variable.
message = await ctx.send(f"{ctx.message.author.mention}, Hi!")
# do what you want with the message, in your case getting it's id
bot_message_id = message.id
As for your second question, if you want to get the response of a user to that message (so the next message a specific user will send), you can use the built-in wait_for for this.
def check(message):
return message.author.id == the_id_of_that_user
message = await bot.wait_for("message", check=check)
That way, the bot will literally wait for a message that makes your check function return True, or in this case a message sent by that very user. More info on that in the relevant API documentation.
I couldn't understand your question very good but I'll try to answer. according to API References you can get someone's last message with using channel.history().get(). So here is a example:
import discord
from discord.ext import commands
async def anyfunc(ctx):
await ctx.send(f"{ctx.message.author.mention}, Hi!")
bot_message_id = await ctx.channel.history().get(author__name="Bot's name").id
This will probably work but in case of error, maybe you can try author__id=bot's id instead of author__name.
I am trying to make a discord bot that sends a DM to someone with a command like:
!messagesteve hello world
and it would send a message directly to the person I want.
I've tried the following, but to no avail:
#client.command(pass_context=True)
async def dm(ctx):
user=await client.get_user_info("381870129706958")
await client.send_message(user, "test")
Any help is appreciated.
It seems as though you're using the old docs of d.py (v0.16.x). The most recent version is on rewrite (v1.x).
One of the changes, amongst others, is that context is automatically implied (you don't need pass_context=True) and the syntax for sending messages has changed, as you'll see in the example:
#client.command()
async def dm(ctx, member: discord.Member, *, message):
try:
await member.send(message)
except: # this will error if the user has blocked the bot or has server dms disabled
await ctx.send("Sorry, that user had DMs disabled!")
else:
await ctx.send(f"Successfully sent {member} a DM!")
The usage, assuming a prefix of !, the usage of the command will be:
!dm #Skyonimous Hey there, I'm DMing you from a bot!
* in the arguments "consumes rest", which means that the argument that succeeds it (message) will act as one whole argument, no matter the number of spaces, so you can send a whole paragraph to a user if you want!
If there's anything that you want me to clarify, I'll be more than happy to!
References:
Major changes 0.16.x -> 1.x
Recent docs
Member.send()
Context.send()