So, when i was trying to add a status to my discord python bot, it wouldn't show up on discord, when running the code no errors would show up, it's just that the status wouldn't show itself.
#Connection to discord
#client.event
async def on_ready():
print({client.user}, 'has connected to Discord!')
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name='~help'))
If anyone knows a fix to this please help.
import discord
import os
import config
import sys
client = discord.Client()
#client.event
async def on_ready():
print({client.user}, 'has connected to Discord!')
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name='~help'))
client.run(config.token) #imports token from the config.py
I tried your code in my test bot and it was working totally fine. I have attached the screenshots below and the code above. Not sure why it wouldn't work for you. [This is my entire code and has nothing extra attached in between or at the end].
Note: import config has my token (so it isn't a necessary import)
Related
My discord bot connects just fine, event on_ready is working with no problems. However when I call any of the commands declared with #bot.command, the bot doesn't recognize the message as a command (at least that's what I think it's happening).
Code:
import discord
import pickle
from discord.ext import commands
from discord.ext.commands import bot
token = ""
bot = commands.Bot(command_prefix='!', case_insensitive=True, intents=discord.Intents.default())
#bot.event
async def on_ready():
print('We have logged in as {0.user}'.format(bot))
#bot.command()
async def test(ctx):
print('received')
await ctx.send('msg received')
bot.run(token)
That's pretty much it, when I run it the bot connects just fine, but the !test command doesn't work.
Let me know if I didn't add important information, first time asking something here.
EDIT: It's working locally, it doesn't work when it's being hosted on heroku (bot connects and is online, doesn't recognize commands).
Requirements file:
git+https://github.com/Rapptz/discord.py
discord~=1.7.3
setuptools~=60.2.0
I've been using the
import os
import discord
from discord.ext import commands
client = commands.Bot(command_prefix = "!")
#client.event
async def on_ready():
await client.change_presence(status=discord.Status.idle,
activity=discord.Game(''))
print('Successfully logged in as {0.user}'.format(client))
#client.command()
async def hello(ctx):
await ctx.send("Hello! It's great to see you!")
client.run(os.getenv('TOKEN'))
code for commands. I expected to have it respond "Hello! It's great to see you!" when I said !hello (! = prefix). But it returned nothing. Not even an error. Does anyone know why? Also, please don't show discord bot not responding to commands or Discord command bot doesn't responde (python) because I tried both, and neither worked.
I cant chat here because of lack of reputation so I answer it here instead. you should post the full code to make the job easier.
.
Cause 1:
You didn't add a prefix. You need a prefix to use the command
look onto your client. Did you tell the prefix already? like:
client = commands.Bot(command_prefix = "-")
if so, add this and it should work!
.
Cause 2:
You didn't import from discord.ext import commands
this is important if you want to use commands, import that and it should work!
.
Cause 3:
You mixing up client and bot
this might not happen but here it goes, You might mix up client or bot on the decorator, which is #client.command
if you use client (like client = commands.Bot(command_prefix = "-")), use #client.command on the decorator, while if you use Bot (bot = commmands.Bot(command_prefix = "-")) then you should use #bot.command on the decorator.
Hope these will help :D
I am trying to code a Discord bot and I would like to add commands so that people with kick/ban permissions can use the kick/ban commands. I have added many different forms of kick and ban commands, but none of them function correctly. I am absolutely positive the kick/ban commands were correct. I am starting to get the feeling that there is something is wrong with the rest of my code, making it where kick/ban commands don't function.
Here is what I currently have:
from discord.ext import commands
import os
from dotenv import load_dotenv
import logging
# Enables logging
logger = logging.getLogger('discord')
logger.setLevel(logging.DEBUG)
handler = logging.FileHandler(filename='discord.log', encoding='utf-8', mode='w')
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
logger.addHandler(handler)
# Assigns the Discord client to a variable called client
client = commands.Bot(command_prefix='$')
# Loads .env
load_dotenv()
# Prints a message stating that the bot is loggen in
#client.event
async def on_ready():
print('Logged in as {0.user}'.format(client))
# Prints the help message when a user says $help
#client.event
async def on_message(message):
if message.content.startswith('$help'):
await message.channel.send('''**Hydra Help**
*This message contains a list of commands and what they do.*
__help__: Prints this message.''')
# Starts the bot
client.run(os.getenv('TOKEN'))
Is there anything wrong with my code so far that may be causing kick/ban commands to not work?
You have to just do this:
Kick:
await ???(For example member or ctx.author).kick(reason=???)
Ban:
await ???(For example member or ctx.author).ban(reason=???)
I'm trying to make a discord bot with discord.py for my discord server, I have extremely basic code that should get the bot online but instead just opens and then crashes.
import discord
#client.event
asnyc def on_ready():
print 'bot read`enter code here`y'
client.run('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
I do have the token normally I just didn't want to leak. I appreciate all the help.
I found a nice tutorial to create a discord bot in python. Therefore you can create a connection with the following code:
import os
import discord
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
client = discord.Client()
#client.event
async def on_ready():
print(f'{client.user} has connected to Discord!')
client.run(TOKEN)
And I would suggest to rename the file from 'discord' to 'bot' or something else, so there are no conflicts with the import.
Hello everyone i was making a bot using discord.py rewrite on pycharm but once there was a error on one of my cogs but the console didn't show any error and it didn't send any message so I had to search a lot about the error. I want to know why its like that.
My main code of the bot is:
import discord
from discord.ext import commands
client = commands.Bot(command_prefix="your prefix")
client.load_extension("extension name")
#client.event
async def on_ready():
print("bot is ready")
client.run('your token
I'm not sure why it doesn't but google will sure help!
You forgot to end the string at client.run so here's the correct code.
import discord
from discord.ext import commands
client = commands.Bot(command_prefix="your prefix")
client.load_extension("extension name")
#client.event
async def on_ready():
print("bot is ready")
client.run('your token')