I want to know if there is away I can access any of the variables num and char from the cog file without having to import them into the main file
Thank for you the help in advance!.
Main file:
bot=commands.Bot(command_prefix='.')
#bot.event
async def on_ready():
print(bot.user.name)
The cog file:
num=123
class Commands:
def __init__(self,bot):
self.bot=bot
char='abc'
def setup(bot):
bot.add_cog(Commands(bot))
Related
As a part of learning OOP, I'm trying to create a class that will handle the loading and unloading cogs. However I'm getting the following error
Traceback (most recent call last):
File "c:\Users\mirza\Desktop\Work\Working Directory\Project\Bots\2B\bot.py", line 50, in <module>
main()
File "c:\Users\mirza\Desktop\Work\Working Directory\Project\Bots\2B\bot.py", line 43, in main
bot = CustomBotClient()
File "c:\Users\mirza\Desktop\Work\Working Directory\Project\Bots\2B\bot.py", line 21, in __init__
raise e
File "c:\Users\mirza\Desktop\Work\Working Directory\Project\Bots\2B\bot.py", line 18, in __init__
self.load_extension(f"cogs.{filename[:-3]}")
File "C:\Users\mirza\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 671, in load_extension
if name in self.__extensions:
AttributeError: 'CustomBotClient' object has no attribute '_BotBase__extensions'
This the code from bot.py that is giving me the error
import discord
from discord.ext import commands
import os
from dotenv import load_dotenv
class CogLoader(commands.Bot):
def __init__(self):
for filename in os.listdir('./cogs'):
if filename.endswith(".py"):
try:
self.load_extension(f"cogs.{filename[:-3]}")
except Exception as e:
print(f"cogs.{filename[:-3]} cannot be loaded")
raise e
#commands.command()
async def load(self, ctx, extension):
self.load_extension(f"cogs.{extension}")
#commands.command()
async def unload(self, ctx, extension):
self.unload_extension(f"cogs.{extension}")
#commands.command()
async def reload(self, ctx, extension):
self.unload_extension(f"cogs.{extension}")
self.load_extension(f"cogs.{extension}")
class CustomBotClient(CogLoader):
async def on_ready(self):
print(f"Bot {self.user} is connected to Discord and ready to roll!")
def main():
bot = CustomBotClient()
load_dotenv()
bot.run(os.getenv("TOKEN"))
if __name__ == "__main__":
main()
This is the cog that I'm trying to load
from discord.ext import commands
class Greetings(commands.Cog):
def __init__(self, bot):
self.bot = bot
#commands.command(name="GreetME")
async def greet_me(self, ctx):
await ctx.send("Hello! {author.user}")
def setup(bot):
bot.add_cog(Greetings(bot))
I'm following a youtube tutorial but he didnot used a class for this. So, I'm a loss here. Any help would be appreciated.
You need to initialize the commands.Bot in your CogLoader.__init__
This can be accomplished through the following code:
class CogLoader(commands.Bot):
def __init__(self, command_prefix, **options):
super().__init__(command_prefix, **options)
for filename in os.listdir('./cogs'):
if filename.endswith(".py"):
try:
self.load_extension(f"cogs.{filename[:-3]}")
except Exception as e:
print(f"cogs.{filename[:-3]} cannot be loaded")
raise e
and
def main():
bot = CustomBotClient(command_prefix="!")
This way, commands.Bot and BotBase, their attributes and their methods are loaded.
I have some code for a cog that looks something like this:
class Example(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.counter = 0
#commands.Cog.listener()
async def on_message(self, message):
print("Listener triggered")
self.counter += 1
#commands.group()
async def first(self, ctx):
if ctx.invoked_subcommand is None:
await ctx.send("Invalid subcommand")
#first.command()
async def second(self, ctx):
print("Command triggered")
await ctx.send(f"Current counter: {self.counter}")
When I run this code and send a message to my bot, second gets called before on_message. I have some code in second that expects on_message to be executed first, but I can't figure out a good way to make this happen. Suggestions?
check wait_for event.
and I think you are trying to make a bot count the number of valid uses of command. To do this, try:
def check(message):
#here put ur condition for message is a valide command
await client.wait_for('message', check=check)
I was using cogs on discord.py to detach the file, but there was a bug that caused NameError and was not running. How can I solve this?
Error
discord.ext.commands.errors.ExtensionFailed: Extension 'cogs.news' raised an error: NameError: name 'news' is not defined.
app.py
...
# Cogs Part
extension = ["cogs.commands","cogs.news", "cogs.manage"]
for filename in os.listdir('./cogs'):
if filename.endswith('.py'):
client.load_extension(f'cogs.{filename[:-3]}')
...
cogs/news.py
...
class news(news.Cog):
def __init__(self, bot):
self.bot = bot
#commands.command(name='news')
async def news(self, ctx):
# ^^^ NameError : name 'news' is not defined
...
If you need any more help after reading this, you can join this small discord server.
I don't know if you left it out intentionally, but you need to add the following code at the end of the cogs/news.py file.
def setup(bot):
bot.add_cog(news(bot))
Also, you shouldn't need to load the extensions like you did, try doing this instead by removing the:
extension = ["cogs.commands","cogs.news", "cogs.manage"]
Like so:
for file in os.listdir("./cogs"):
if file.endswith(".py"):
name = file[:-3]
client.load_extension(f"cogs.{name}")
If none of this works consider replacing the
class news(news.Cog):
With:
class news(commands.Cog):
EXTRAS:
You can also add a command on the app.py file that allows you to reload any command in the cogs without restarting the bot:
#client.command()
#commands.is_owner()
async def reload(ctx, *, name: str):
try:
client.reload_extension(f"cogs.{name}")
except Exception as e:
return await ctx.send(e)
await ctx.send(f'"**{name}**" Cog reloaded')
And an option that alows you to disable or enable a command:
#client.command()
#commands.is_owner()
async def load(ctx, *, name: str):
try:
client.load_extension(f"cogs.{name}")
except Exception as e:
return await ctx.send(e)
await ctx.send(f'"**{name}**" Cog loaded')
And
#client.command()
#commands.is_owner()
async def unload(ctx, *, name: str):
try:
client.unload_extension(f"cogs.{name}")
except Exception as e:
return await ctx.send(e)
await ctx.send(f'"**{name}**" Cog unloaded')
If this help in any way, an upvote and a mark as answer would be appreciated :)
I was making an discord bot and im new to python and i have looked up but didn't find any answer on how to fix this but i got error from this code
Code:
import discord
from discord.ext import commands
client = command.Bot(command_prefix="<")
#client.event
async def on_ready():
print("Bot is online")
#client.command()
synd def Test(ctx):
await ctx.send("Test successful")
client.run(Token)
line with error:
#client.event
async def on_ready():
print("Bot is online")
Error:
async def on_ready():
^
SyntaxError: invalid syntax
Does anyone know how to fix this? i am using Python version: pip 19.2.3 (python 3.8)
EDITED
The print function needs to be intended another 4 spaces to be like:
async def on_ready():
print("Bot is online")
you can try use command "python3 file_name.py"
I have a flip command that returns either heads or tails and it was working fine until I added the on_message function. I did some research on SO and read the documentation and here it says to include await bot.process_commands(message) in the on_message function but that did not solve the issue and the flip command still doesn't work. If I remove the on_message function everything works as expected.
bot.py
import discord
from discord.ext import commands
import random
# from app bot user
# click to reveal
TOKEN = 'token_here'
client = commands.Bot(command_prefix = '!')
#client.event
async def on_ready():
print('Bot ready')
#client.command()
async def flip():
flip = random.choice(['heads', 'tails'])
await client.say(flip)
#client.event
async def on_message(message):
if message.content.upper().startswith('N****'):
await client.send_message(message.channel, "Please do not use that word here.")
client.process_commands(message)
#client.event
async def on_member_join(member):
await client.change_presence(game=discord.Game(name='Hi %s' % (member)))
await client.send_message(member, "Hi %s, Welcome to Carson's Discord Server! This server is fairly NSFW at times; you've been warned! Enjoy your stay :)" % (member))
#client.event
async def on_member_remove(member):
await client.change_presence(game=discord.Game(name='Bye %s' % (member)))
client.run(TOKEN)
When I run the script, everything else works normally with no errors. It's just the commands that don't work. Any insight would be greatly appreciated.
You need to await the last line in on_message as process_commands is a coroutine.
await client.process_commands(message)
If you send !flip in discord, you should be able to receive the appropriate response (heads or tails).