Blocking commands in a certain discord.py channel - python

So I recently added this code
#bot.event
async def on_message(message):
prefix = re.findall('([;]|[-]|[=]+)', message.content.lower())
if prefix and message.channel.id == "405815888177266689":
await bot.delete_message(message)
The bot does remove the message but the bot detects the command too fast so the other bot(s) reply. I am wanting to make it where the other bots can't reply. What I'm asking is - is it possible to basically add the purge to this command to make it purge the recent 2 messages (the command + the bots reply).

You could do something like this
#bot.event
async def on_message(message):
prefix = re.findall('([;]|[-]|[=]+)', message.content.lower())
if prefix and message.channel.id == "405815888177266689":
await bot.delete_message(message)
muted_bots = ['bot_1_id','bot_2_id']
async for msg in bot.logs_from(bot.get_channel("405815888177266689"), limit=4):
if msg.author.id in muted_bots:
await bot.delete_message(msg)
where after someone writes a message with that prefix and it gets deleted, it checks for the last number of messages (whatever you put the limit to depending on how many bots respond) and checks if whoever wrote that is in your muted_bots id list.

Related

Discord.py- How to make bot send a message when pinged but not when replied to

Image Link I'm trying to make something where if the bot is pinged it says a message like "For a list of commands, type .help" and my code works except for it also says that when the bot is replied to.
This is my code - Using discord.py 1.7.3
#client.event
async def on_message(message):
if client.user.mentioned_in(message) and message.mention_everyone is False:
await message.channel.send("For a list of commands, type `.help`")
await client.process_commands(message)
You're looking for client.user.mention. You can also check if client.user.mention in message.content if you want to know whether the bot is mentioned somewhere in the message rather than checking if the mention comprises the entire message.
#client.event
async def on_message(message):
if client.user.mention == message.content and message.mention_everyone is False:
await message.channel.send("For a list of commands, type `.help`")
await client.process_commands(message)
Reference:
abstractmethod mention

Discord.py Translate message contents from Channel to Specific User ID's DMS

I am trying to create a bot that if it detects "$" before a message, to then take that entire messages content, and send it to a specific users dms. (user id removed for security)
async def on_message(message):
if message.content.startswith('$ '):
CONTENT = message.content
async def sendDm():
user = await client.fetch_user("530916313972080652")
await user.send(CONTENT)
However, upon doing this, the bot does not respond to any message period.
I have tried multiple send.message functions, all to no avail, most saying not defined.
I got assistance from the Discord.py Discord.
#client.event
async def on_message(message):
if message.content and message.content.startswith("$") and not message.author.bot:
user = await client.fetch_user(user_id)
await user.send(message.content)

how to make my program skip the "on_message" function if false (discord.py rewrite)

I want to make my bot be able to tag members when a certain command is written, but the only way I was able to do that is with a on_message command which seems to just stop my program whether false or true, even if it activates when true
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('m-example'):
await message.channel.send('<' + '#' + '{}' .format(message.author.id) +'>' + ' example!')
I am assuming, that by stopping the program you meant that the bot commands don't work. This is because your on_message overrides discord.py's default on_message which processes your commands. Here is what you need to do
#client.event
async def on_message(message):
await client.wait_until_ready()
# do stuff here
await client.process_commands(message)
wait_until_ready() makes sure that your bot does not do anything till it is connected and ready
process_commands tells discord.py to process the message as a command
References:
wait_until_ready
process_commands
Extra tips:
You can know if your bot's script has stopped or if something else is wrong has happened by checking if the bot is online/ has the status you used when starting it.
You need to put await client.process_commands(message) in your script for your other commands to work.
I recommend using message.author.mention instead of '<' + '#' + '{}' .format(message.author.id) +'>' which is much easier.
So your script should look like this;
#client.event
async def on_message(message):
await client.process_commands(message)
if message.author == client.user:
return
if message.content.startswith('m-example'):
await message.channel.send(message.author.mention + ' example!')
Firstly you can do mentioning someone much easier, for this example with message.author.mention, secondly I don't know what problem you have but maybe this now works:
#client.event
async def on_message(message):
if message.author.id != bot.user.id:
if message.content.startswith('m-example'):
await message.channel.send(f"{message.author.mention} example!")

How to make Discord.py respond to a single message event twice

I have a discord bot that can read all messages in a channel and add each message to a line in a .txt file, then respond confirming it did. I then want to have the bot wait 10 seconds then delete the original message and the response. I have code that can make this work. I had both working together as intended on my test bot. But when I copied the code to my main bot it wouldn't work and no longer would my test bot. The code I am posting is the test bot, right now it only deletes the original message as intended. If you leave out the second #bot.event chunk it responds and records the message as intended. How can I make both of these occur from the same bot? (The request channel bit is is from a failed attempt to have the code specify what channel the bot operates in rather than just close off each channel individually in discord). Both bots are in separate channels so I don't believe it is the bots actions interfering with eachother?
import discord
from discord.ext import commands
import asyncio
requestsChannelID = 'CHANNELID'
bot = commands.Bot(command_prefix='!')
#bot.event
async def on_message(message):
if message.author == bot.user:
return
with open('requests.txt', 'a') as f:
print(repr(message.content), file=f)
await message.channel.send('Got it!')
#bot.event
async def on_message(message):
await asyncio.sleep(10)
if str(message.channel) == "requests" and message.content != "":
await message.channel.purge(limit=1)
bot.run('TOKEN')
You can use discord.Message.delete to specifically delete both the request and the reply, no need to use purge (which quickly gets very complex when there are multiple messages within the 10s time frame)
#bot.event
async def on_message(message):
if message.author == bot.user:
return
# ignore all channels but the specified one
if message.channel.id == requestsChannelID:
# record request
with open('requests.txt', 'a') as f:
print(repr(message.content), file=f)
reply = await message.channel.send('Got it!')
# wait for 10 seconds, then delete message and reply
await asyncio.sleep(10)
await message.delete()
await reply.delete()
else:
# process other commands (if there are none, delete this line)
await bot.process_commands(message)
Note that this implementation assumes there will be no commands in the requests channel (which from my understanding is what you intended, anyway)

Discord Bot Delete Messages in Specific Channel

TIA for your help and apologies, I'm a newbie so this may be a foolish question. I've searched through and can't find anything specific on how to make a discord bot (in Python) delete messages only within a specific channel. I want all messages sent to a specific channel to be deleted, their contents sent via PM to the user and the user's role changed.
Is there a way to use on_message and specify in an specific channel?
#client.event
async def on_message(message):
user = message.author
if message.content.startswith("Cluebot:"):
await message.delete()
await user.send("Yes?")
await user.remove_roles(get(user.guild.roles, "Investigator"))
The problem I'm having is I'm also using commands which now no longer work because the bot only responds if the message begins with "Cluebot:" Can I have the bot only look for "Cluebot:" in a specific channel?
Is it possible to make this work through a command instead of an event?
Thanks for your help. :)
The problem I'm having is I'm also using commands which now no longer work because the bot only responds if the message begins with "Cluebot:" Can I have the bot only look for "Cluebot:" in a specific channel?
About this problem the clue is:
await bot.process_commands(message)
as docs says Without this coroutine, none of the commands will be triggered.
about the main question you could try using get_channel by ID and then purge it:
eg.
#client.event
async def on_message(message):
purgeChannel = client.get_channel([CHANNEL ID]])
await purgeChannel.purge(limit=1)
you can add check to purge() to check for deleting specific message:
eg.:
def check(message):
return message.author.id == [author's ID]
#client.event
async def on_message(message):
purgeChannel = client.get_channel([CHANNEL ID]])
await purgeChannel.purge(limit=1, check=check)

Categories

Resources