I have problem with definition of webhook - python-3.x

So here's the problem I don't now how to define a webhook message in if message == Here's my code:
webhook_urls = ['url1', 'url2]
#bot.event
async def on_message(message):
channels = ["global"]
if message.author.id == bot.user.id:
return
if message == DiscordWebhook:
return
for word in channels:
await message.delete()
response = DiscordWebhook(url=webhook_urls, content=message.content).execute()
It should return the webhook.

You can check if the message has a webhook_id
if message.webhook_id is not None:
...

Related

Discord bot not recognizing messages

I'm new to creating bots and I wanted to try and make a bot that would greet me when I said hi and say goodbye when I said bye.
When I activated it, it seemed to not be able to recognize that I sent a message
I checked permissions and it should be able to see it but it doesn't look like it can
here's the code
import os
import discord
client = discord.Client()
#client.event
async def on_ready():
print("We are {0.user} and we are on a cruise".format(client))
#client.event
async def on_messsage(message):
username = str(message.author).split('#')[0]
user_message = str(message.content)
channel = str(message.channel.name)
print(f'{username}: {user_message} ({channel})')
if message.author == client.user:
return
if channel == 'general':
if user_message.lower() == 'hello':
await message.channel.send(f'Hello {username}')
return
elif user_message.lower() == 'bye':
await message.channel.send(f'Bye {username}')
return
client.run(os.environ['taisho-secret'])
#client.event
async def on_message(message):
username = str(message.author).split('#')[0]
user_message = str(message.content)
channel = str(message.channel.name)
print(f'{username}: {user_message} ({channel})')
if message.author == client.user:
return
if channel == 'general':
if user_message.lower() == 'hello':
await message.channel.send(f'Hello {username}')
return
elif user_message.lower() == 'bye':
await message.channel.send(f'Bye {username}')
return
your problem is that you wrote on_messsage and its on_message

Translation with reactions not working on python

I'm trying to do a bot translation using reactions. Although something is not working.
#commands.Cog.listener()
async def on_reaction_add(self, reaction, user):
if user == self.client:
return
if reaction.emoji == ":flag_us:":
text = reaction.message.id
translate_text = google_translator.translate(text, lang_tgt='en')
await self.client.send_message(translate_text.channel)
elif reaction.emoji == ":flag_cn:":
text = reaction.message.id
translate_text = google_translator.translate(text, lang_tgt='zh')
await self.client.send_message(translate_text.channel)
else:
return
No error returned and no action made
This is because reaction.emoji isn't a string, but is an object itself. You're probably looking for reaction.emoji.name.
Also, there are a few issues within the if/elif clauses that would prevent your code from running, even if the original issue was fixed.
reaction.message.id is an integer, so passing it to google_translator.translate() will result in an error.
The name of an emoji tends not to be the name you would enter in Discord. The best practice would be to put the unicode of the emoji.
To send a message to channel, you should use TextChannel.send()
#commands.Cog.listener()
async def on_raw_reaction_add(self, payload):
if payload.member == self.client:
return
if payload.emoji.name == u"\U0001f1fa\U0001f1f8":
message = await self.client.fetch_message(payload.message_id)
translate_text = google_translator.translate(message.content, lang_tgt='en')
channel = await self.client.fetch_channel(payload.channel_id)
await channel.send(translate_text)
elif payload.emoji.name == u"\U0001F1E8\U0001F1F3":
message = await self.client.fetch_message(payload.message_id)
translate_text = google_translator.translate(message.content, lang_tgt='zh')
channel = await self.client.fetch_channel(payload.channel_id)
await channel.send(translate_text)
This would work, but I would recommend taking all of the various calls outside of the if/elif clauses:
#commands.Cog.listener()
async def on_raw_reaction_add(self, payload):
if payload.member == self.client:
return
emoji_to_language = {
u"\U0001f1fa\U0001f1f8": "en",
u"\U0001F1E8\U0001F1F3": "zh"
}
lang = emoji_to_language.get(payload.emoji.name)
if lang is None:
break
message = await self.client.fetch_message(payload.message_id)
translate_text = google_translator.translate(message.content, lang_tgt=lang')
channel = await self.client.fetch_channel(payload.channel_id)
await channel.send(translate_text)

in discord.py how to to delete others all message

I want to make bot that delete specific server or channel's bot's all message.
but I can find just deleting event message or delete channel's message.
import asyncio
#client.event
async def on_message(message):
content = message.content
guild = message.guild
author = message.author
channel = message.channel
if content == "!delete":
await #delete all bot's message
Deleting all the bot's message in a specific channel can be done with channel.purge
async def on_message(message):
channel = message.channel
if message.content == '!delete':
if len(message.mentions) == 0:
await channel.purge(limit=100, check= lambda x: x.author.id == client.user.id)
else:
mentioned = message.mentions[0]
await channel.purge(limit=100, check= lambda x: x.author.id == mentioned.id)
If you want to delete messages in all channels, just loop it with guild.channels
References:
channel.purge
message.mentions
Note:
The check function should return a bool that tells use whether we should delete the message or not, and takes the message as the parameter.
If you want to delete all messages in specific channel:
guilds_list = [CHANNEL_ID, CHANNEL2_ID, CHANNEL3_ID]
#client.event
async def on_message(message):
if message.channel.id in guilds_list:
await message.delete()
Now bot will delete all messages in specific channel, if you want to delete only bot's messages:
#client.event
async def on_message(message):
if message.channel.id in guilds_list and message.author is client.user:
await message.delete()
Note: this process is automatic. So bot will delete every message it send to specific channel.

Check if message reply is a reply type message discord.py

I have the following basic python discord bot code:
#bot.command()
async def replyTest(ctx):
await ctx.send('Reply to this message')
def check(m):
return m
msg = await bot.wait_for("message", check=check)
print(msg)
Is there a way to return m only when m is a reply type message?
You can simply check if the message has a reference.
def check(m):
if m.reference is not None and not m.is_system :
return True
return False
Additionally if you want to check if the reference points to a message
def check(m):
if m.reference is not None:
if m.reference.message_id = some_msg.id
return True
return False
References:
message.reference
Reference.message_id

Reaction Handling in Discord.py Rewrite Commands

Is there a way to capture a reaction from a command. I have made a command that deletes a channel but I would like to ask the user if they are sure using reactions. I would like to prevent others from reacting to this message (Only the Context Author should React).
So far what I have found is just to use the on_reaction_add() but this can not detect the user who sent the command. I would like to only update the command if the message author is the one who reacted to the message, anyone else, ignore it.
Update: I found that wait_for() does exactly what I want but the issue now is how do I check for if the wrong reaction is set? (i.e if I press the second reaction, delete the message)
if is_admin:
msg = await ctx.send('Clear: Are you sure you would like to purge this entire channel?')
emoji1 = u"\u2705"
emoji2 = u"\u274E"
await msg.add_reaction(emoji=emoji1)
await msg.add_reaction(emoji=emoji2)
def check(reaction, user):
return user == ctx.message.author and reaction.emoji == u"\u2705"
try:
reaction, user = await self.client.wait_for('reaction_add', timeout=10.0, check=check)
except asyncio.TimeoutError:
return await msg.delete()
else:
channel = ctx.message.channel
new_channel = await channel.clone(name=channel.name, reason=f'Clear Channel ran by {ctx.message.author.name}')
await new_channel.edit(position=channel.position)
await channel.delete(reason=f'Clear Channel ran by {ctx.message.author.name}')
await new_channel.send('Clear: Channel has now been cleared.', delete_after=7)
else:
await ctx.send(f"Sorry, you do not have access to this command.", delete_after=5)
Here's a function I use for generating check functions for wait_for:
from collections.abc import Sequence
def make_sequence(seq):
if seq is None:
return ()
if isinstance(seq, Sequence) and not isinstance(seq, str):
return seq
else:
return (seq,)
def reaction_check(message=None, emoji=None, author=None, ignore_bot=True):
message = make_sequence(message)
message = tuple(m.id for m in message)
emoji = make_sequence(emoji)
author = make_sequence(author)
def check(reaction, user):
if ignore_bot and user.bot:
return False
if message and reaction.message.id not in message:
return False
if emoji and reaction.emoji not in emoji:
return False
if author and user not in author:
return False
return True
return check
We can pass the message(s), user(s), and emoji(s) that we want to wait for, and it will automatically ignore everything else.
check = reaction_check(message=msg, author=ctx.author, emoji=(emoji1, emoji2))
try:
reaction, user = await self.client.wait_for('reaction_add', timeout=10.0, check=check)
if reaction.emoji == emoji1:
# emoji1 logic
elif reaction.emoji == emoji2:
# emoji2 logic
except TimeoutError:
# timeout logic

Resources