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!')
Related
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).
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 wanted to try my hand at making a discord bot however I'm running into an issue where my bot will not come online. I've tried 2 different codes I've found from internet tutorials but neither of them will make the bot appear online or do anything. I've seen someone ask a similar question on here using a similar code to what I have implemented and was currently working with and said that their issues were coming from a certificate error (Discord Bot not coming online) but I'm not sure how I would go about resolving it. For context I'm using Python 3.9 on PyCharm and my code looks like this (I have used an actual token but I've heard its not good to give that out) :
import discord
from discord.ext import commands
client = commands.Bot(command_prefix='.')
#client.event
async def on_ready():
print('bot is ready')
client.run('TOKEN')
Please ask if you need any more information that what I've already provided.
edit1:pycharm says that the import 'import discord' is unused is that why im having issues?
edit2:I have tried using the following command to get the bot to do something but it is still not working-
#client.event
async def ping(ctx):
await ctx.send('pong!')
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.
I'm trying to get my discord.py bot hosted for free, and I heard Heroku is a good option. My dyno is working perfectly, and it's switched on, but when I deploy, my bot does not go online. I have no errors, it says it's deployed, but my bot never goes online.
I have tried regenerating the bot token, and putting it in my Heroku app, but it still won't work.
import discord
from discord.ext import commands
from discord.ext.commands import bot
import asyncio
import requests
import os
description = '''EchoBot by EchoNoahGaming'''
bot = commands.Bot(command_prefix='-', description=description)
#bot.event
async def on_ready():
print('Logged in as')
print(bot.user.name)
print(bot.user.id)
print('------')
#bot.command()
async def announcement(ctx, *, args):
"""Announcement command!"""
embed=discord.Embed(title="Announcement", description=args, color=0x7700aa)
embed.set_footer(text="By EchoNoahGaming")
await ctx.send("#everyone", embed=embed)
client.run(str(os.environ.get('BOT_TOKEN')))
That is the bot code.
I expected the bot to come online, because there were no errors, but it didn't, and it stayed offline.
The Procfile code is
worker: python3 bot.py
The requirements.txt content is
discord
asyncio
If you need anything else, tell me. The GitHub link is https://github.com/EchoNoahGaming/echobot/blob/master/ but i'll post any files on here so it's easier.
From the discussion in the comments, OP and I figured out what went wrong:
There is a typo on the last line: client.run should be bot.run
The requirements.txt file should be changed to
discord.py
requests
This is because requests isn't part of the Python standard library whereas asyncio is part of the standard library.
Fixing the above seemed to have solve the issues.
An auxiliary code review would be to remove redundant lines, such as
from discord.ext.commands import bot
(which gets overwritten by bot = Bot(...) anyway).
The asyncio and requests modules weren't used in the code, but I'll give you the benefit of the doubt and assume you're planning on using them. If not, remove them from your code (and, if applicable, from requirements.txt). This saves time in building the bot.