I'm trying to make this simple bot with some functions.
Currently, I am stuck on this problem. I want to welcome new members and have a command for changing nicknames on the server. Usually, I try every function standalone, and they work on their own. Whenever I try to combine them into one big functional bot, I run into trouble.
I am using Bot and Client at the same time, which probably causes problems. Unfortunately, I don't know how to rewrite the bot command into client command and vice versa. This is the code I'm working with:
intents = discord.Intents.default()
intents.members = True
client = discord.Client()
#client.event
async def on_member_join(member):
print("member join")
channel = client.get_channel(929370329363644506)
#embed=discord.Embed(title="Welcome!",description=f"{member.mention} Just Joined")
await channel.send(f"{member.mention} Welcome to our server! Please check <#556206910794366988> and set your name and rank based on your ingame name and rank")
bot = Bot("!")
#bot.command()
#commands.has_permissions(change_nickname=True)
async def nick(ctx, *, nickname):
member = ctx.author
await member.edit(nick=nickname)
await ctx.send(f"Nickname was changed to {member.mention}.")
my_secret = os.environ['TOKEN']
bot.run(my_secret)
With that, welcoming the members doesn't work; changing nick is fine.
The first thing I tried was to change the #client.event to #bot.event and define bot as bot = discord.Client() but that didn't help.
I would prefer to write the whole bot with #bot.
I am guessing I'm missing the fundamentals a bit, but if there is someone who can point me in the right direction, I would appreciate that.
You never run the client. Since the client and bot are different variables you need to run them both seperately. So, just add client.run(my_secret) to your code.
P.S. I highly recommend you to use only Bot as it has all the functionality of a discord client but also supports commands, command groups, cogs, etc. Also as Lukasz said you never add your intents (and while it won't throw any error if you don't define them it's generally better practice to do so).
Related
I'm trying to make a discord bot say a certain message when it first joins a Discord Server, so when the bot first joins a Discord Server, it will say something along the lines of "Hello everyone....". I looked at a lot of sources but none seem to be,Can anyone help?
make a bot say a certain message when it first joins a server
Heyo.
To make your bot say something in a channel, you just use a client event.
Code example:
#client.event
async def on_guild_join(guild):
await channel.send("Wassup!")
Please keep in mind that you need to define the channel variable.
Remember that this requires Intents.guilds to be enabled!
You can check all of this on the discord.py docs. https://discordpy.readthedocs.io/en/stable/api.html?highlight=event#discord.on_guild_join
I haven't been looking for an answer to this question for a long time, but I closed it and the help on the browser history didn't help in any way. I tried to do it myself, but it didn't work out. I wanted to make it so that if the bot did not have rights, then he wrote about it and if there were, then he did the action, please help!
Note: This message is written assuming you are using the standard discord.py events and not the bot events, but it obviously also works with the bot events. Just replace "client" with "bot" and instead of "message" use "ctx.message".
You can get your client user via
client.user
To get the member object of the client user, you can use
message.channel.guild.get_member(client.user.id)
Documentation
On member objects you can then call guild_permissions
member.guild_permissions
Documentation
Permission objects are documented here.
Here is an example that sends a message if the bot cannot react to messages.
#client.event
async def on_message(message):
client_member = message.channel.guild.get_member(client.user.id)
if not client_member.guild_permissions.add_reactions:
await message.channel.send("I cannot add reactions in this channel")
return
I want to create a bot that will respond to commands with arguments. I have gotten it to respond to strings in messages, but when i try to make commands working, the bot seems not to react at all. I have studied the documentation, put a lot of effor into trying to find the answer on the internet, but no help. My code can be the exact same as someone else's on the internet, but the bot never responds, while still responding to messages. I wonder if there's something really basic that I'm missing about how the commands are supposed to work.
I have imported the right library, I use the exact same syntax many people are sharing on the internet, yet the commands just don't do anything. I use replit.com which is a browser IDE.
This snippet of code is like half the code from the bot, he's quite dumb so far. Everything i tried other than commands has worked so far. I usually also don't work with Python, but i just followed the syntax i saw in tutorials to not make mistakes. When i run this, the bot logs in successfully, then if i send !tst the bot just doesn't do anything.
from discord.ext import commands
bot = commands.Bot(command_prefix='!')
#bot.command(name='tst')
async def test(ctx):
await ctx.send('testt')
I mainly used this tutorial: https://realpython.com/how-to-make-a-discord-bot-python/#how-to-make-a-discord-bot-in-python
Also the library documentation: https://discordpy.readthedocs.io/en/stable/index.html
import discord
import os
from discord.ext import commands
bot = commands.Bot(command_prefix='!')
#bot.command(name='tst')
async def test(ctx):
await ctx.send('testt')
client = discord.Client()
#client.event
async def on_ready():
print ('Successful login as {0.user}'.format(client))
client.run(os.getenv('TOKEN'))
I didn't want to post the whole thing, but it's really nothing special so here it goes. for a moment i thought that might be the solution, but even if i clear it of all message related code, it still does nothing. Could it be conflicting with the on_ready message? That one just goes to the console on the IDE website..
Based on the full code:
You shouldn't use both discord.Client and commands.Bot, because you are defining two different bots, but run only one of them. commands.Bot extends the discord.Client. Think about commands.Bot as a subclass of discord.Client. Everywhere where you've used client before, now you can use bot.
The code you posted would run correctly like this:
import discord
import os
from discord.ext import commands
bot = commands.Bot(command_prefix='!')
#bot.command(name='tst')
async def test(ctx):
await ctx.send('testt')
#bot.event
async def on_ready():
print ('Successful login as {0.user}'.format(bot))
bot.run(os.getenv('TOKEN'))
Original answer:
(it might be a solution to someone else looking for it)
Are you using the on_message event? That prevents the command processor from getting the messages. You have to call the command processor manually, like so:
#bot.event
async def on_message(message):
# Do your stuff
await bot.process_commands(message)
According to the documentation you should call this function when you override the on_message event.
By default, this coroutine is called inside the on_message() event. If you choose to override the on_message() event, then you should invoke this coroutine as well.
I have a code that's simple as this
bot = discord.Client()
#bot.event
async def on_connect():
print('Hello!')
however, when i run the code it won't print Hello! and it doesn't give any error message, and the bot itself did not even turn online
i can't solve it myself since i couldn't find any info online, or it might be that im just dumb ._.
It's not on_connect, instead, try using on_ready
Your code you have provided also seems to refer to the old async version, instead of rewrite, running your bot and defining it should be:
bot = commands.Bot(command_prefix = prefix)
Your bot also may not be online as you might of missed to actually run your bot via it's token and declaring it.
bot.run('token')
Also make sure you have imported Discord's modules, otherwise everything should be working. It's best if you start by reading and learning about Discord's documentation guide, as this provides the simple, basic and crucial information in setting up your bot, to avoid these small errors. A guide can be found here:
https://discord.com/developers/docs/intro
import discord
import discord.utils
from discord.ext import commands, tasks
#client.event
async def on_ready():
print('Hello!')
I am trying to program a Discord bot in Python using the discord.py module.
When I run the code, it doesn't say anything, waits a little (probably importing/connecting stuff) and then closes - or in IDLE, >> appears.
There is no problem with the code, as it doesn't give any errors. Plus, if I run a command out of on_ready() it works as expected.
So the problem isn't because of not being able to connect to the bot or Discord, is it? Perhaps the asnyc function doesn't work, or the interpreter doesn't recognize it?
Here is the basic form of the main code:
import discord
token = 'blahblah'
client = discord.Client()
#client.event
async def on_message(message):
if message.content=="some input":
msg = 'some output'
await message.channel.send(msg)
#client.event
async def on_ready():
print("All works fine!")
client.run(token)
Since you mention something about it being an issue with async I shall leave an answer that I gave another user about async. https://stackoverflow.com/a/63005581/13950887
Another issue I had recently was due to discord updating their website from discordapp.com and discord.gg to discord.com, a way I found to resolve this was to edit a bunch of the files, one thing I will say is that I wouldn't recommend doing this unless you know what you are doing.