Discord Bot command not functioning - python

from discord.ext import commands
from apikeys import *
intents = discord.Intents.default()
intents.message_content = True
client = commands.Bot(command_prefix='$', intents=intents)
dictionary = ["aaa"]
#client.event
async def on_ready():
print(f'We have logged in as {client.user}')
#client.command()
async def hello(ctx):
print("test")
await ctx.send("Hello, I am bot")
client.run(BOTTOKEN)
When I run the code it says
We have logged in as demo-python-v2#1499
meaning that its not a connectivity issue.
What could be the problem?

from discord.ext import commands
bot = commands.Bot(command_prefix='$')
#bot.command()
async def test(ctx):
pass
# or:
#commands.command()
async def test(ctx):
pass
bot.add_command(test)
you need to register the command itself or add it. here is an example

Related

Discord Bot not responding to commands... (Using Pycord)

Im trying to make a discord bot, and I want its first command to be pretty simple, I just want it to respond "pong!" when someone types "!ping" but when I try to run the command, nothing happens.. I've tried using other commands too but they all result in errors.. I'm a beginner to using Python, so please, can someone tell me what's wrong with my code, and how I can fix it?
In short, my bot doesn't respond back when I type the command I made for it to do so.
import discord
import os
from discord.ext import commands
from discord.ext.commands import Bot
case_insensitive=True
client = discord.Client()
bot = commands.Bot(
command_prefix="!",
intents=discord.Intents(members=True, messages=True, guilds=True)
)
#client.event
async def on_ready():
print('logged in as {0.user}!'.format(client))
#client.event
async def on_message(message):
if message.author == client.user:
return
#bot.command(name="ping", description= "Find out!")
async def ping(ctx):
await ctx.send("Pong!")
client.run(os.getenv('TOKEN'))
Instead of #bot.command you should use #client.command.
Also no need for the from discord.ext.commands import Bot import
So your new code would be
import discord
import os
from discord.ext import commands
case_insensitive=True
client = discord.Client()
bot = commands.Bot(
command_prefix="!",
intents=discord.Intents(members=True, messages=True, guilds=True)
)
#client.event
async def on_ready():
print('logged in as {0.user}!'.format(client))
#client.event
async def on_message(message):
if message.author == client.user:
return
#client.command(name="ping", description= "Find out!")
async def ping(ctx):
await ctx.send("Pong!")
client.run(os.getenv('TOKEN'))
You could just do inside on_message code block
import discord
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)
#client.event
async def on_ready():
print(f'We have logged in as {client.user}')
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('!ping'):
await message.channel.send('Pong!')
client.run('your token here')

Why can't my discord.py bot see messages?

I am making a discord.py bot and it does not "see" messages at all, it is supposed to print everything into the terminal and have a prefix, but it doesn't even print any message.
code:
import datetime
import os
import discord
from timeit import default_timer as timer
from dotenv import load_dotenv
from discord.ext import commands
load_dotenv()
bot = commands.Bot(command_prefix="!")
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix="!", intents=intents)
TOKEN = ('boop')
client = discord.Client()
#client.event
async def on_ready():
print(f'{client.user} has connected to Discord!')
async def on_message(message):
print(f'USER - {message.author} texted - {message.content}')
#bot.command()
async def start(ctx):
await ctx.channel.send("you have started")
await ctx.guild.create_role(name= {message.author} + "w")
client.run(TOKEN)
(there are extra unused modules I'm planning to use later)
There are a few problems in your code:
You should only have one commands.Bot instance, events are not exclusive to discord.Client.
For your bot to catch commands, you need bot.process_commands at the end of your on_message event.
You were missing #bot.event above on_message.
You forgot the fstring in ctx.guild.create_role(...).
import discord
from discord.ext import commands
import os
import datetime
from dotenv import load_dotenv
from timeit import default_timer as timer
load_dotenv()
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix="!", intents=intents)
#bot.event
async def on_ready():
print(f'{client.user} has connected to Discord!')
#bot.event
async def on_message(message):
print(f'USER - {message.author} texted - {message.content}')
await bot.process_commands(message)
#bot.command()
async def start(ctx):
await ctx.channel.send("You have started")
await ctx.guild.create_role(name=f"{message.author}w")
bot.run("TOKEN")

Discord.py Why will this client.command not work?

import discord
from discord.ext import commands
import asyncio
client = commands.Bot(command_prefix="-")
#client.event
async def on_message(message):
if message.content.startswith("uno regeln"):
await message.channel.send('http://www.uno-kartenspiel.de/spielregeln/')
#client.command()
async def test(ctx):
await ctx.send("Ok")
client.run("TOKEN")
When I try to run this and use -test nothing happens
But why?
I looked in the docs but in my opinion everything is fine
As given by the link provided by #Lukasz KwieciƄski,
you need to change it to:
import discord
from discord.ext import commands
import asyncio
client = commands.Bot(command_prefix="-")
#client.event
async def on_message(message):
if message.content.startswith("uno regeln"):
await message.channel.send('http://www.uno-kartenspiel.de/spielregeln/')
await bot.process_commands(message)
#client.command()
async def test(ctx):
await ctx.send("Ok")
client.run("TOKEN")
import discord
from discord.ext import commands
intents = discord.Intents.default()
client = commands.Bot(command_prefix="!", intents=intents)
client.remove_command('help')
#client.event
async def on_ready():
print("Bot Is Ready")
await client.change_presence(activity=discord.Game(name="TYPE THERE ANYONE"))
#client.command()
async def test(ctx):
await ctx.send("Ok")
client.run('YOUR TOKEN')
Try this

discord.py command in cogs file not working

Everything used to work fine when all code was in main.py but then I created cogs folder and created multiple cog files.py and moved code around, now ?servers command is not working. I get
discord.ext.commands.errors.CommandNotFound: Command "servers" is not found
import discord
from discord.ext import commands
intents = discord.Intents.default()
intents.members = True
intents.presences = True
client = commands.Bot(command_prefix='?', intents=intents, help_command=None)
#client.event
async def on_ready():
print('ARB is logged in.')
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name='#ARB'))
client.load_extension("cogs.commands")
client.load_extension("cogs.owner")
client.load_extension("cogs.fun")
client.load_extension("cogs.arb")
cogs/owner.py
import discord
from discord.ext import commands
client = commands.Bot
class Owner(commands.Cog):
def __init__(self, client):
self.client = client
#commands.command
#commands.is_owner()
async def servers(self, ctx):
guildcount = 0
activeservers = client.guilds
print('********************** GUILD LIST **********************')
for guild in activeservers:
guildcount += 1
print(f'{guild.name} - ({guild.id})')
print(f'ARB is in {guildcount} servers.')
def setup(client):
client.add_cog(Owner(client))
You don't have to define client again in the extension, simply delete that line. Also the decorator #commands.command should be called (with ())
import discord
from discord.ext import commands
class Owner(commands.Cog):
def __init__(self, client):
self.client = client
#commands.command()
#commands.is_owner()
async def servers(self, ctx):
guildcount = 0
activeservers = client.guilds
print('********************** GUILD LIST **********************')
for guild in activeservers:
guildcount += 1
print(f'{guild.name} - ({guild.id})')
print(f'ARB is in {guildcount} servers.')
def setup(client):
client.add_cog(Owner(client))

Discord py not recognizing command

client.event works fine, connection and messages are registered. But cannot get discord to recognize command when I type i.e. '.ping'
python 3.7 on anaconda distribution
import os
import discord
from discord.ext import commands
from dotenv import load_dotenv
load_dotenv('.env.txt')
TOKEN = os.getenv('DISCORD_TOKEN')
client = commands.Bot(command_prefix = '.')
#client.event
async def on_ready():
print(f'{client.user} has connected to Discord!')
#client.event
async def on_message(message):
print(f'{client.user} has sent a message')
#client.command()
async def ping(ctx):
print('test')
await ctx.send('test')
client.run(TOKEN)
Alright, you need to understand that overridng the default on_message forbids any extra commands from running.
An easy fix for this, is to add a client.process_commands(message) to the end of the on_message event.
So in your case:
import discord
from discord.ext import commands
from dotenv import load_dotenv
load_dotenv('.env.txt')
TOKEN = os.getenv('DISCORD_TOKEN')
client = commands.Bot(command_prefix = '.')
#client.event
async def on_ready():
print(f'{client.user} has connected to Discord!')
#client.event
async def on_message(message):
print(f'{client.user} has sent a message')
client.process_commands(message)
#client.command()
async def ping(ctx):
print('test')
await ctx.send('test')
client.run(TOKEN)```

Categories

Resources