Non functioning cogs | DiscordPy 2.0 - discord

I would like to use the cog to make my code cleaner but my cog "ping" does not want to work. The bot goes online but on my discord server I can't find my slash command.
Thanks for your help!
My code :
File main.py
import discord
from discord.ext import commands
from discord import app_commands
bot = commands.Bot(command_prefix = "//", intents = discord.Intents.all())
bot.load_extension("cogs.ping")
#bot.event
async def on_ready():
print(f"{bot.user} is online !")
bot.run('TOKEN')
File ping.py
import discord
from discord.ext import commands
from discord import app_commands
class Ping(commands.Cog):
def __init__(self, bot):
self.bot = bot
#commands.command()
async def ping(self, ctx):
ctx.send(f"Pong in {self.latency * 1000}ms")
def setup(bot):
bot.add_cog(Ping(bot))
My hierarchy :
mybot
main.py
cogs
ping.py

Extension loading is now async, as the error message (that you should be getting, but didn't include in the post) suggests.
Migration guide: https://discordpy.readthedocs.io/en/stable/migrating.html#extension-and-cog-loading-unloading-is-now-asynchronous
on my discord server I can't find my slash command
PS there are no slash commands here, your ping is a regular message command. Even after loading the cog there won't be any slash commands, because you didn't make any.

Related

Module 'discord' has no attribute 'Bot' in pycord version 2.3.2

I'm trying to switch from discord.py to pycord since I heard that they don't support slash commands, and while defining simple commands. I keep getting this great Traceback
`
Traceback (most recent call last):
File "c:\Users\****\source\repos\DBot\main.py", line 16, in <module>
bot = discord.Bot()
AttributeError: module 'discord' has no attribute 'Bot'
`
This is my code up until now.
`
import discord
import os
from dotenv import load_dotenv
# importing environmental vars
load_dotenv()
TOKEN = os.getenv("DISCORD_TOKEN")
GUILD = os.getenv("DISCORD_GUILD")
APP_ID = os.getenv("APP_ID")
PUB_KEY = os.getenv("PUBLIC_KEY")
GUILD_ID = os.getenv("GUILD_ID")
GENERAL_CHAN_ID = os.getenv("CHANNEL_GENERAL_ID")
#finished importing .env vars
#class for client
bot = discord.Bot()
#bot.slash_command()
async def hello(ctx, name: str = None):
name = name or ctx.author.name
await ctx.respond(f"Hello {name}!")
#bot.user_command(name="Say Hello")
async def hi(ctx, user):
await ctx.respond(f"{ctx.author.mention} says hello to {user.name}!")
bot.run("token")
`
I have py-cord version 2.3.2 so I know it's not version issue
I just started using py-cord so I have no idea what I'm doing except ctrl c ctrl v-ing examples from their documentation page to see if it works
Help is greatly appreciated!!!
I had the same mistake.
What fixed it for me was:
Go into your project directory.
pip install py-cord if this doesn't work, try step 3.
pip install py-cord==2.0.0rc1 this is a beta version of pycord.

Discord bot missing 'Intents'

I am currently trying to make a discord bot in python that responds to a command. When I run the code it prints
Traceback (most recent call last):
File "d:\Python\Projects\disco bot\ppap", line 4, in <module>
client = discord.Client()
TypeError: __init__() missing 1 required keyword-only argument: 'intents'
This is the code:
import discord
import os
client = discord.Client()
#client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$hello'):
await message.channel.send('Hello!')
client.run(os.getenv('DiscordBotToken'))
how would I fix this?
I have looked up for a fix but nothing is working.

Cannot load extensions/cogs in Discord.py anymore on version

Code (most of it is hidden):
import discord
from discord import app_commands
from discord.ext import commands
intents = discord.Intents.default()
bot = discord.Client(
intents=intents,
description='REDACTED',
case_insensitive=True,
owner_id=REDACTED
)
#bot.event
async def on_ready():
print(f'Logged in as {bot.user.name} - {bot.user.id}')
bot.load_extension(cogs.Mod)
bot.run("token")
Error:
Traceback (most recent call last):
File "C:\Users\Personal\discord-bots\quantum.py\quantum.py", line 18, in <module>
bot.load_extension(cogs.Mod)
^^^^^^^^^^^^^^^^^^
AttributeError: 'Client' object has no attribute 'load_extension'
I was trying to be able to use the <bot/client>.load_<extension/cog>() prompt, but it didn't work all of a sudden.
On the current version of discord.py (2.1.0), you can only call load_extension on a discord.ext.commands.Bot (see https://discordpy.readthedocs.io/en/stable/ext/commands/api.html?highlight=load_extension#discord.ext.commands.Bot.load_extension)
As per the API documentation:
This class is a subclass of discord.Client and as a result anything that you can do with a discord.Client you can do with this bot.
So you shouldn't have any trouble replacing discord.Client with a commands.Bot in your existing code.

client = discord.Client() TypeError: Client.__init__() missing 1 required keyword-only argument: 'intents' How do I fix this? New to python

Hey I'm new to python and I'm trying to create a multifunctional discord bot my bot is called flea. Anyway I encountered this error but I don't know how to fix it, I'm usually relatively quick to understand errors but I can't seem to figure this out. This is what my code looks like. The code is intended to make my bot go from offline to online on discord (running) by running the "on_ready" function
The following images is what my code looked like after I tried to fix the issue, the change did nothing but remove the error, the bot still won't appear online on discord: screenshot 1, screenshot 2
Below is my current code in my bot.py file, with the same error
> # imports import discord # importing discord import responses # importing responses.py file
>
>
> async def send_message(message, user_message, is_private):
> try:
> response = responses.response_handle(user_message)
> await message.author.send(response) if is_private else await message.channel.send(response)
> except Exception as e:
> print(e)
>
>
> def run_flea():
> token = 'my token'
> client = discord.Client() # here is the error
>
> #client.event
> async def on_ready(): # this is when the bot gets started, it will call the "on_ready" function
> print(f'{client.user} is now running') # this tells us that our bot is up and ready
>
> client.run(token)
This is because you need to declare the intents as the parameter for the client variable just insert this parameter and it should work:
client = discord.Client(intents=discord.Intents.default())

Discord.py Buttons Error unexpected keyword

Here is my code:
async def test(ctx):
await ctx.send(
"Discord Buttons!",
components=[
[Button(style=ButtonStyle.red, label="Red"),
Button(style=ButtonStyle.red, label="Red", disabled=True),
Button(style=ButtonStyle.URL, label="Youtube", url="https://youtube.com")],
Button(style=ButtonStyle.blue, label="Blue", emoji="😅"),
],
)
res = await bot.wait_for("button_click")
if res.channel == ctx.message.channel:
await res.repond(
type=InteractionType.ChannelMessageWithSource,
content=f"Pressed!"
)
And here is the error it keeps on giving:
Command raised an exception: TypeError: send() got an unexpected keyword argument 'components'
I am trying to make a command where if you say $test, it responds with Buttons: https://support.discord.com/hc/article_attachments/1500019725621/buttons.png
It seems like you are trying to use discord-components. For this, you will need to install the package with pip install discord-components and then add the following to your code if you haven't done so.
from discord.ext.commands import Bot
from discord_components import DiscordComponents, Button
bot = Bot('$') # Prefix
#bot.event
async def on_ready():
DiscordComponents(bot)
# ...
# ...
Then from there you should be able to add a components field to send with Buttons in it.

Resources