I am running this script that I found from a YouTube tutorial, and it was working earlier. Now it seems to not be working anymore.
Script that isn't working
import asyncio
import discord
from discord.ext import commands
client = commands.Bot(command_prefix='', help_command=None, self_bot=False)
class SelfBot(commands.Cog):
def __init__(self, client):
self.client = client
#commands.command()
async def sendMessage(self, ctx):
await ctx.send("Send Message")
client.add_cog(SelfBot(client))
client.run(token, bot=False)
it seems to be as if it is getting "stuck" at #commands.command() and won't run the sendMessage function. My bot also appears to be online once the script is running. Any ideas on how to fix it?
Another thing that i found interesting is that one of my scripts that i created does work how it was intended.
Script that is working
#bot.event
async def on_message(message):
await asyncio.sleep(1)
with message.channel.typing():
await asyncio.sleep(2)
await message.channel.send("test")
return
bot.run(token, bot=False)
This will message me test once I send a message to the server.
Your command might not be working because you have a on_message event. on_message events have the priority on commands and if you don't process them, none of them will be able to be executed.
What you have to add is in your on_message event:
#bot.event
async def on_message(message):
await asyncio.sleep(1)
with message.channel.typing():
await asyncio.sleep(2)
await message.channel.send("test")
await bot.process_commands(message)
If it doesn't solve the problem, it might also come from your commands.Bot variable. For an unkown reason, you seem to have two commands.Bot variables: client and bot. You should only have one.
Also, I saw you set your prefix as '', if you don't want any error like Command not found, you should set a prefix (eg. !, $, ;, ...).
Also, as #InsertCheesyLine recommended it, you should separate your cogs in different files and put them in a folder nammed cogs, like so:
Main file (eg. bot.py)
from discord.ext import commands
bot = commands.Bot(command_prefix='!')
extensions = ['cogs.test'] #cogs.(filename)
if __name__ == '__main__':
for extension in extensions:
bot.load_extension(extension)
#bot.event
async def on_ready():
print(f'Bot is ready to go!')
bot.run('TOKEN')
One of your cogs (eg. cogs/test.py)
from discord.ext import commands
#This is a cog example, you'll have to adapt it with your code
class Test(commands.Cog):
def __init__(self, bot):
self.bot = bot
#commands.Cog.listener() #equivalent to #bot.event
async def on_message(self, message):
if 'test' in message.content:
await message.channel.send('Test Message')
await self.bot.process_commands(message)
#commands.command()
async def ping(self, ctx):
await ctx.send('Pong!')
def setup(bot):
bot.add_cog(Test(bot))
Related
I've started to try coding a discord bot but it was cut short when setting a prefix and events wouldn't work. It just displays an error message that says 'the command "hello" can not be found'.
import discord
from discord.ext import commands
import asyncio
client = commands.Bot(command_prefix="*" ,status=discord.Status.idle, activity=discord.Game("Starting up..."))
#client.event
async def on_ready():
await client.change_presence(status=discord.Status.do_not_disturb, activity=discord.Game("Preparing Puzzle Event"))
print("Bot is running.")
#client.event
async def hello(ctx):
await ctx.send("Hi")
client.run('[The token]')
I know that the token is valid as I've ran it before without assigning a prefix and using 'async'.
#client.event
async def hello(ctx):
await ctx.send("Hi")
This won't work because hello is not a built-in event. See: https://discordpy.readthedocs.io/en/latest/api.html#discord-api-events
What you want is a command probably
#client.command()
async def hello(ctx):
await ctx.send("Hi")
This registers the bot to respond to the command .hello assuming . is your prefix
Basically, everything appears to work fine and start up, but for some reason I can't call any of the commands. I've been looking around for easily an hour now and looking at examples/watching videos and I can't for the life of me figure out what is wrong. Code below:
import discord
import asyncio
from discord.ext import commands
bot = commands.Bot(command_prefix = '-')
#bot.event
async def on_ready():
print('Logged in as')
print(bot.user.name)
print(bot.user.id)
print('------')
#bot.event
async def on_message(message):
if message.content.startswith('-debug'):
await message.channel.send('d')
#bot.command(pass_context=True)
async def ping(ctx):
await ctx.channel.send('Pong!')
#bot.command(pass_context=True)
async def add(ctx, *, arg):
await ctx.send(arg)
The debug output I have in on_message actually does work and responds, and the whole bot runs wihout any exceptions, but it just won't call the commands.
From the documentation:
Overriding the default provided on_message forbids any extra commands from running. To fix this, add a bot.process_commands(message) line at the end of your on_message. For example:
#bot.event
async def on_message(message):
# do some extra stuff here
await bot.process_commands(message)
The default on_message contains a call to this coroutine, but when you override it with your own on_message, you need to call it yourself.
Ascertained that the problem stems from your definition of on_message, you could actually just implement debug as a command, since it appears to have the same prefix as your bot:
#bot.command()
async def debug(ctx):
await ctx.send("d")
As described in the title, I am new to Python(programming in general) and I tried making a bot, however the bot does not respond to commands. I followed/looked through multiple youtube tutorials & articles, but I cannot find a way to fix my problem.
import discord
from discord.ext.commands import Bot
bot = Bot(".")
#bot.event
async def on_ready():
print("kram is now online")
await bot.change_presence(activity=discord.Game(name="This bot is a WIP"))
#bot.event
async def on_message(message):
if message.author == bot.user:
#bot.command(aliases=["gp"])
async def ghostping(ctx, amount=2):
await ctx.send("#everyone")
await ctx.channel.purge(limit = amount)
#bot.command()
async def help(ctx):
await ctx.send("As of right now, .gp is the only working command.")
bot.run("I'm hiding my token")
Hey why don't you try this instead the same thing but i removed the on message
import discord
from discord.ext.commands import Bot
bot = Bot(".")
#bot.event
async def on_ready():
print("kram is now online")
await bot.change_presence(activity=discord.Game(name="This bot is a WIP"))
#bot.command(aliases=["gp"])
async def ghostping(ctx, amount=2):
await ctx.send("#everyone")
await ctx.channel.purge(limit = amount)
#bot.command()
async def help(ctx):
await ctx.send("As of right now, .gp is the only working command.")
bot.run("I'm hiding my token")
This should work as when using cogs and when you have a command there is no need to put it in the on_message event. i suggest you watch this series(Really helpful while starting out):
https://www.youtube.com/watch?v=yrHbGhem6I4&list=UUR-zOCvDCayyYy1flR5qaAg
Basically, everything appears to work fine and start up, but for some reason I can't call any of the commands. I've been looking around for easily an hour now and looking at examples/watching videos and I can't for the life of me figure out what is wrong. Code below:
import discord
import asyncio
from discord.ext import commands
bot = commands.Bot(command_prefix = '-')
#bot.event
async def on_ready():
print('Logged in as')
print(bot.user.name)
print(bot.user.id)
print('------')
#bot.event
async def on_message(message):
if message.content.startswith('-debug'):
await message.channel.send('d')
#bot.command(pass_context=True)
async def ping(ctx):
await ctx.channel.send('Pong!')
#bot.command(pass_context=True)
async def add(ctx, *, arg):
await ctx.send(arg)
The debug output I have in on_message actually does work and responds, and the whole bot runs wihout any exceptions, but it just won't call the commands.
From the documentation:
Overriding the default provided on_message forbids any extra commands from running. To fix this, add a bot.process_commands(message) line at the end of your on_message. For example:
#bot.event
async def on_message(message):
# do some extra stuff here
await bot.process_commands(message)
The default on_message contains a call to this coroutine, but when you override it with your own on_message, you need to call it yourself.
Ascertained that the problem stems from your definition of on_message, you could actually just implement debug as a command, since it appears to have the same prefix as your bot:
#bot.command()
async def debug(ctx):
await ctx.send("d")
Basically, everything appears to work fine and start up, but for some reason I can't call any of the commands. I've been looking around for easily an hour now and looking at examples/watching videos and I can't for the life of me figure out what is wrong. Code below:
import discord
import asyncio
from discord.ext import commands
bot = commands.Bot(command_prefix = '-')
#bot.event
async def on_ready():
print('Logged in as')
print(bot.user.name)
print(bot.user.id)
print('------')
#bot.event
async def on_message(message):
if message.content.startswith('-debug'):
await message.channel.send('d')
#bot.command(pass_context=True)
async def ping(ctx):
await ctx.channel.send('Pong!')
#bot.command(pass_context=True)
async def add(ctx, *, arg):
await ctx.send(arg)
The debug output I have in on_message actually does work and responds, and the whole bot runs wihout any exceptions, but it just won't call the commands.
From the documentation:
Overriding the default provided on_message forbids any extra commands from running. To fix this, add a bot.process_commands(message) line at the end of your on_message. For example:
#bot.event
async def on_message(message):
# do some extra stuff here
await bot.process_commands(message)
The default on_message contains a call to this coroutine, but when you override it with your own on_message, you need to call it yourself.
Ascertained that the problem stems from your definition of on_message, you could actually just implement debug as a command, since it appears to have the same prefix as your bot:
#bot.command()
async def debug(ctx):
await ctx.send("d")