Reputation: 33
The problem is that all the code inside the main file and the cog file is correct although for some reason on_message()
doesn't work. I do not get any exception while running this, but it does not do anything in discord.
This is the main code, this also loads the cogs.
import discord
from discord.ext import commands
from discord.utils import get
import os
from dotenv import load_dotenv
load_dotenv('.env')
token = os.getenv('TOKEN')
bot = commands.Bot(command_prefix='$')
client = discord.Client()
@client.event
async def on_ready():
print(' Made by Termed#6382')
print(' Bot events are logged below : ')
print(' ----------')
for file in os.listdir('./cogs'):
if file.endswith('.py') and not file.startswith('_'):
bot.load_extension(f'cogs.{file[:-3]}')
print(' Loaded {}'.format(file))
client.run(token)
bot.run(token)
This is the cog file, inside a cog folder
import discord
import os
from discord.ext import commands
from discord.utils import get
import asyncio
client = discord.Client()
class onMessage(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.Cog.listener()
async def on_message(message):
message_content = message.content
if message_content.isupper():
if message.author.id == 'XX':
await message.channel.send('{}No u'.format(message.author.mention))
message_author = message.author
await message.channel.send('{} , Please refrain from using too many capital letters.'.format(message.author.mention))
print(' Warned {} for too many capital letters'.format(message_author))
def setup(bot):
bot.add_cog(onMessage(bot))
Upvotes: 0
Views: 202
Reputation: 3497
You don't need to use both bot = commands.Bot(command_prefix='$')
and client = discord.Client()
. These will both create separate instances of Discord bots, with bot
using the commands
extension.
Remove client
and use bot
throughout your code. You also don't need to specify client = discord.Client()
in your cog.
main
from discord.ext import commands
import os
from dotenv import load_dotenv
load_dotenv('.env')
token = os.getenv('TOKEN')
bot = commands.Bot(command_prefix='$')
@bot.event
async def on_ready():
print(' Made by Termed#6382')
print(' Bot events are logged below : ')
print(' ----------')
for file in os.listdir('./cogs'):
if file.endswith('.py') and not file.startswith('_'):
bot.load_extension(f'cogs.{file[:-3]}')
print(' Loaded {}'.format(file))
bot.run(token)
cog
from discord.ext import commands
class onMessage(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.Cog.listener()
async def on_message(message):
message_content = message.content
if message_content.isupper():
if message.author.id == 'XX':
await message.channel.send('{}No u'.format(message.author.mention))
message_author = message.author
await message.channel.send('{} , Please refrain from using too many capital letters.'.format(message.author.mention))
print(' Warned {} for too many capital letters'.format(message_author))
def setup(bot):
bot.add_cog(onMessage(bot))
Upvotes: 2