My bot will send messages when other bots send messages - discord

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

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.

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.

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.

discord.py Cooldown error doesn't allow other errors to be raised

I have this code for cooldowns and to send the error to the user. This is my code:
#bot.event
async def on_command_error(ctx, error):
if isinstance(error, commands.CommandOnCooldown):
if error.retry_after>3600:
embed=discord.Embed(title='Cooldown!', description=f"This command can be used in **{round(error.retry_after/3600)}h** !".format(error.retry_after))
await ctx.send(embed=embed)
return
if error.retry_after>60:
embed=discord.Embed(title='Cooldown!', description=f"This command can be used in **{round(error.retry_after/60)}m**!".format(error.retry_after))
await ctx.send(embed=embed)
return
else:
embed=discord.Embed(title='Cooldown!', description=f"This command can be used in **{round(error.retry_after)}s**!".format(error.retry_after))
await ctx.send(embed=embed)
But when i add this, other errors such as MissingRequirements don't get raised in the terminal in Visual Studio Code. Does anyone know why or how this can be fixed?
The Error Handler checks if the error is the CommandOnCooldown error. If it isnt, the error gets ignored. Add another else statement:
#bot.event
async def on_command_error(ctx, error):
if isinstance(error, commands.CommandOnCooldown):
if error.retry_after>3600:
....
else:
....
else:
Stuff the bot should do on a different error come here

Resources