im trying to make a bot for my discord server, but all my commands dont work.
im using windows and pycharm to test and use the bot. i tried many different types but nothing works. im using python 3.7
import discord
from discord.ext import commands
import asyncio
from discord.ext.commands import Bot
Client = discord.Client()
client = commands.Bot(command_prefix='.')
#client.event
async def on_ready():
print("bot is active")
#client.command()
async def ping(ctx):
await ctx.send('pong')
await print("pong")
it doesn't crash or give an error, it just doesn't do anything in the command
Try this:
import discord
from discord.ext import commands
TOKEN = YOUR_TOKEN_GOES_HERE
client = commands.Bot(command_prefix = '.')
#client.event
async def on_ready():
print("Powering up the bot")
#client.event
async def on_message(message):
print("A new message!")
await client.process_commands(message)
#client.command()
async def ping(ctx):
await ctx.channel.send("Pong!")
Related
I've just started learn python and my discord bot won't go online. it just said
" Process exit with exit code 0". And there's no error with the code.
here's my code. enter image description here
Add await client.process_commands(ctx) and #client.event don't need parentheses(). Use the code given below:
#client.event
async def on_message(ctx):
if ctx.author == client.user:
return
await client.process_commands(ctx)
Use this entire code if it still not working:
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix="!")
#bot.event
async def on_ready():
print('We are logged in!')
#bot.event
async def on_message(message):
if message.author==bot.user:
return
await bot.process_commands(message)
#bot.command()
async def ping(message) :
await message.channel.send("Pong!!")
bot.run("TOKEN")
You should try this instead
import os
import discord
from discord.ext import commands
discord_token = "Your Token"
client = discord.Client()
bot = commands.Bot(command_prefix="s!")
#bot.event
async def on_ready():
print("running")
bot.run(discord_token)
#bot.command()
async def on_ready():
print("running")
bot.run(discord_token)
and the output should be running
You should try this instead
import os
import discord
from discord.ext import commands
discord_token = "Your Token"
client = discord.Client()
bot = commands.Bot(client_prefix="!")
#client.command()
async def on_ready():
print("running")
bot.run(discord_token)
The code you gave is wrong, this is correct:
import os
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix="!") # you don't need an extra discord.client Bot is enough
token = os.getenv("TOKEN")
#you should not run the program here you should run it at last
#bot.event #no need for brackets
async def on_ready():
print("running")
#bot.event
async def on_message(ctx):
if ctx.author == client.user:
return
#bot.command(name="span",description="YOUR DESCRIPTION HERE") # it is command and not commands
async def span_(ctx,amount:int,*,message):
for i in range(amount):
await ctx.send(message)
bot.run(token) #you should run the bot now only and this will work perfectly!
You can find the the documentation for discord.py here.
i have a problem with my discord bot: when i type on discord $ping he doesn't response me pong and i don't know why, i just check the bot have the admin role so he can write, i'm using VsCode and he didn't give me nothing error.
Here is the code
import discord
from discord.ext import commands
import requests
import json
import random
client = discord.Client()
bot = commands.Bot(command_prefix='$')
#bot.command()
async def ping(ctx):
await ctx.channel.send("pong")
#client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
client.run("XXXXXXXXXXXXXXXXXXXXXXX")
The problem is, that you are defining the command with bot.command, but you only do client.run. To fix this, either choose a client, or a bot, but not both, like this if you choose bot for example:
import discord
from discord.ext import commands
import json
import random
bot = commands.Bot(command_prefix='$')
#bot.command()
async def ping(ctx):
await ctx.channel.send("pong")
#bot.event
async def on_ready():
print('We have logged in as {0.user}'.format(bot))
bot.run(Token)
Also don't use requests, since it's blocking.
Okay so I am designing a Discord bot in Python, am pretty new though. I was trying to get it to join a voice channel so I did some research and came up with the following, I cut out the irrelevant part of the bot so this is just the part with the voice connection. It says: undefined name "commands" over the line that should set the prefix:
import discord
import os
import requests
import json
import random
client = discord.Client()
client = commands.Bot(command_prefix='!')
#client.event
async def on_ready():
print("Eingeloggt als {0.user}".format(client))
#bot.command()
async def join(ctx):
channel = ctx.author.voice.channel
await channel.connect()
#bot.command()
async def leave(ctx):
await ctx.voice_client.disconnect()
client.run(os.getenv("TOKEN"))
You must use from discord.ext import commands
import discord
import os
import requests
import json
import random
from discord.ext import commands
bot = commands.Bot(command_prefix='!')
#bot.event
async def on_ready():
print("Eingeloggt als {0.user}".format(client))
#bot.command()
async def join(ctx):
channel = ctx.author.voice.channel
await channel.connect()
#bot.command()
async def leave(ctx):
await ctx.voice_client.disconnect()
bot.run(os.getenv("TOKEN"))
I've been researching methods to add cooldowns to only specific commands in my bot. I am unable to find anything that works, and I'm unsure how #commands.cooldown(rate=1, per=5, bucket=commands.BucketType.user) would be implemented into this code.
import discord
import random
from pathlib import Path
from discord.ext import commands
from discord.ext.commands.cooldowns import BucketType
client = discord.Client()
#client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('#help'):
*command goes here*
if message.content.startswith('#gamble'):
*command goes here*
client.run('TOKEN')
I suggest you using commands.Bot as it has all the functionality that discord.Client has + a full command system, your code rewrited would look like this:
import discord
from discord.ext import commands
# You should enable some intents
intents = discord.Intents.default()
bot = commands.Bot(command_prefix="#", intents=intents)
bot.remove_command("help") # Removing the default help command
#bot.event
async def on_ready():
print(f"Logged in as {bot.user}")
#bot.command()
async def help(ctx): # Commands take a `commands.Context` instance as the first argument
# ...
#bot.command()
#commands.cooldown(1, 6.0, commands.BucketType.user)
async def gamble(ctx):
# ...
bot.run("TOKEN")
Take a look at the introduction
This is my code:
import discord
import asyncio
from discord import Game
from discord.ext import commands
bot = commands.Bot(command_prefix="!")
#bot.event
async def on_ready():
print(bot.user.id)
print("botstart")
game = discord.Game("abc")
await bot.change_presence(status=discord.Status.online, activity=game)
#bot.event
async def on_message(message):
if message.content.startswith("hi"):
await message.channel.send("Hello")
if message.content.startswith("ping"):
await message.channel.send("Pong")
#bot.command
async def ping(ctx):
ctx.send("Pong!")
bot.run("(my token)", bot=True)
For some reason, bot.event works but #bot.command(!ping) part doesn't. I tried to fix it but i couldn't, even watching discord.py guides.
What am I doing wrong?
Since you are overriding the on_message event, the bot no longer responds to any commands. To fix this add await bot.process_commands(message) to your on_message function for it to handle commands normally.
Source:
https://discordpy.readthedocs.io/en/latest/faq.html#why-does-on-message-make-my-commands-stop-working