Discord bot missing 'Intents' - discord

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.

Related

Non functioning cogs | DiscordPy 2.0

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.

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.

My bot will send messages when other bots send messages

I have my discord bot log messages as they get sent by users in my server, but whenever other bots send messages I want the bot to not log there messages. How do I do this
Log code:
#client.event
async def on_message(ctx):
if ctx.author != client.user:
log_msg = client.get_channel(1003447126501625856)
await log_msg.send(f"""**Message sent by: {ctx.author.mention} in {ctx.channel.mention}.
Message:** "{ctx.content}" """)
print(f"""**Message sent by: {ctx.author} in {ctx.channel}.
Message:** "{ctx.content}" """)
You should check if the incoming author of the message is a bot. If so, return None.
async def on_message(ctx):
# return if the message is from a bot
if ctx.author.bot: return # noqa: E701
Sources:
discord.py 2.0 -> Github
discord.py 2.0 -> Documentation

pycord send discord.ui.View object to specific channel

I'm trying to send View object to specific channel.
I've tried this :
#bot.event
async def on_ready():
ch = _bot.get_channel(927913185766436885)
await ch.purge(limit = 100)
v = discord.ui.View()
v.add_item(Vrb())
await ch.send(view=v)
There is view in ch.send's hint, But when i put view=v to ch.send, It raises:
Traceback (most recent call last):
File "C:\Users\CENSORED\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 352, in _run_event
await coro(*args, **kwargs)
File "CENSORED", line 2344, in on_ready
await ch.send(view=v)
File "C:\Users\CENSORED\AppData\Local\Programs\Python\Python39\lib\site-packages\discord_components\dpy_overrides.py", line 350, in send_override
return await send(channel, *args, **kwargs)
TypeError: send() got an unexpected keyword argument 'view'
how can i fix this?
i don't want to use slash commands or things.
Edit : I'm using version py-cord-2.0.0a4739+g128a9e97.
Uninstall discord.py or any other discord.py fork if you have any, then install the Development version of Pycord.
pip install -U git+https://github.com/Pycord-Development/pycord

Im Trying to create a custom bot status but im getting this error again and again

This is my code:
#client.event
async def on_ready():
await client.change_presence(status=discord.Status.idle, activity=discord.Game('test" '))
But I'm getting this error:
Ignoring exception in on_ready
Traceback (most recent call last):
File "C:\Users\XaiZaiRo\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "e:\coding\Discord Bots\Undercons\bot\main.py", line 30, in on_ready
await client.change_presence(status=discord.Status.idle , activity=discord.Game('Managing "discord.gg/crepling" '))
AttributeError: 'Command' object has no attribute 'Status'
I'm pretty sure this is what you are looking for.
client = Bot(command_prefix=">",
help_command=None,
case_insensitive=True,
max_messages=100,
activity=Activity(type=ActivityType.watching,
name=f"over your server."),
intents=discord.Intents.all())
It will basically say "Watching over your server." as an activity.

Resources