Reputation: 374
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.
Upvotes: 0
Views: 649
Reputation: 3994
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))
Upvotes: 1