I want to send to the user a change log message when someone uses the bot!
Is there something like on_command when someone uses the bot they get a dm?
Yes, you can use either before_invoke or after_invoke depending on when you want your log message to run.
#client.before_invoke
async def before_invoke(ctx):
await ctx.send('before invoke')
#client.after_invoke
async def after_invoke(ctx):
await ctx.send('after invoke')
await ctx.author.send('Command completed!')
Related
This bot is only going to be for personal use, so I don't need to make it customizable from discord itself. I was wondering if I can use ctx.send and channel.send somehow together if this makes sense. Here is my code to help you understand what I mean:
client = commands.Bot(command_prefix='!')
channel = client.get_channel(883389016819511296)
#client.command(name='say')
async def say(ctx, *, content):
await ctx.send(content) # here I want to send it to the channel I already defined
channel.send would be an option, but I also want to send the message, so if I am right I need to use ctx.
No, you need to use channel.send as you are sending the content to the specified channel. If you use ctx.send it will just send in the same channel as the command was executed in. And theres an easier way to do this if you wanna see and example i have one here
#client.command() async def say(ctx, channel: discord.TextChannel, *, message: str): await channel.send(message)
That is an easier way and you can "#" the text channel so for example if you want to send a message in general for your bot it would be: !say #general hello
However if you only wish to do that and not use my example then you should do it like this:
#client.command(name='say') async def say(ctx, *, content): channel = client.get_channel(883389016819511296) await channel.send(content)
I want to make a bot that sends me DM, but it has error
It is my code:
member = client.get_user(int(my id))
await member.send('abc')
And error:
AttributeError: 'NoneType' object has no attribute 'send'
Make sure you replace '1234567890' with your own user ID
user = client.get_user(1234567890)
await user.send("Hi!", tts=True)
Example -
#client.command()
async def dm(ctx):
user = client.get_user(1234567890)
await user.send("Hi!", tts=True)
Before you do anything
First, make sure the bot is in some way connected to you, whether that be you share a guild or you already have a DM channel open with the bot. Second, make sure you allow DMs from shared server members or strangers so that the bot's DM won't be blocked.
Code
Instead of naming your variable "member", let's name it something more appropriate since you are retrieving a User object instead of a Member. Next, you want to test if you and the bot share a DM channel, if not the bot will create one, and then you can send the message.
user = client.get_user(int('0123456789'))
if not (dm_channel := user.dm_channel):
dm_channel = await user.create_dm()
await dm_channel.send('abc')
I've got an issue but I am getting no errors and from the code I just wrote as shown below
#client.commands
async def hello():
channel = int(797915093954199565)
await channel.send('Hey what are you doing?')
I am trying to make a command where the user can talk to the bot, and it responds back with something, its just a starting command but I'm having trouble with these small things, the rest of the bot works but its just this issue i'm having please help!
So assuming the rest of your bot and code works, your "hello" command isn't working because you have
#client.commands #its client.command in this case, as it seems you are not using any cogs, and you must call the client with () these double parentheses
async def hello(): #Here you have not passed ctx
channel = int(793744805615632394) #It seems what you would like to do here is send it to a specific channel, however, in the code below, I have set it so it just sends to the channel it was used in. The correct use is client.get_channel(793744805615632394)
await channel.send('Hey what are you doing?')
Here is the command but fixed:
So assuming the rest of your bot and code works, your "hello" command isn't working because you have
#client.command()
async def hello(ctx):
await ctx.send('Hey what are you doing?')
You should also refer to the py docs to learn more about it: https://discordpy.readthedocs.io/en/latest/
I am trying to make a bot that sends the user who entered the command a DM, but I don't get an error code in PyCharm or a DM in Discord when I enter the command. Am I doing something wrong?
#client.command(aliases=['manage'])
async def oversee(message):
await message.author.send('Hello World!')
According to the discord.py documentation, when you create a command using the commands framework, your first argument must be ctx:
#client.command(aliases=['manage'])
async def oversee(ctx):
await ctx.author.send('Hello World!')
Your function would work as intended if it was a on_message event:
#client.event
async def on_message(message):
if message.content.startswith("!manage") or message.content.startswith("!oversee"):
await message.author.send('Hello World!')
However, I don't recommand using on_message events to create commands, using the commands framework will be way easier and more efficient.
So I'm new to Python and I decided to take the plunge and make a Discord bot for personal use in my server. I like the idea of having full control over what features my bot will have so I'm slowly building the bot. Currently I want my bot to show the current number of members in the server when called with a command
import discord
from discord.ext import commands
#botbot.command()
async def server(ctx):
guild = discord.Guild.member_count
await ctx.send(guild)
I know I'm most likely way off with my code here.
While the bot is sending a message into the chat, its formatted as:
<property object at 0x046036C0>
While I would like to have it say something like "This server has {some number} members."
Any advice is really appreciated!
Thank you for your time.
Edit: botbot is the name for my bot, just so thats clear.
discord.Guild is the class. You want to get the guild the command is in from the invocation context:
#botbot.command()
async def server(ctx):
await ctx.send(f"This server has {ctx.guild.member_count} members.")
The method member_count is not really what you want in this occasion, the guild object has a list called members, so it's as easy as getting the length of that list like so:
#botbot.command()
async def server(ctx):
guild = len(discord.guild.members)
await ctx.send(guild)
EDIT: And indeed you have a typo with using "Guild" instead of "guild"