Discord PY Bot not responding to an event - url-rewriting

I have this bot that when it detects that a user has written a certain message in a certain channel, it will send a message then delete the user's message and its own message.
I wrote the exact same function to do the same thing in another channel and it works just fine.
I checked the bot's permissions and it can send messages in the channel,
here is the code:
the for loop is for searching for a specific text.
#client.event
async def on_message(message):
text = message.content
chann = message.channel.id
yes = False
for strf in comms:
if text.startswith(strf):
yes = True
if yes and (message.author.id != 769302967803183124) and (message.channel.id == 331562608467509249):
print(message.author.id)
msg = await message.channel.send('mech lahne tekteb el commands ye bhim')
await message.delete()
await asyncio.sleep(4)
await msg.delete()

Related

I want to my discord bot to forward the message to another channel after a user reacts to it Discord.py

I know next to nothing about coding in Python but I created this bot from tutorials I found online and it does little things in my private server.
One thing we'd love to do is to react to certain messages so they will be collected in a specific channel. I've seen a few servers do this, like a highlights kind of thing.
I found something promising here in Stackoverflow but it's in javascript :<
Edit: 2023-01-19 08:53 GMT+8
#client.event
async def on_reaction_add(reaction, member):
if str(reaction.emoji) == "⭐":
channel = await bot.get_channel(channel_id)
await channel.send(reaction.message.content)
Final code
edited: 2023-01-20 17:14 GMT+8
#bot.event
async def on_reaction_add(reaction, user):
if reaction.emoji == "⭐":
astrometrics = bot.get_channel(channel id here)
embed = discord.Embed(color = 0xFEE75C)
embed.set_author(name = reaction.message.author.name, icon_url = reaction.message.author.display_avatar)
embed.add_field(name = "Message Content", value = reaction.message.content)
if len(reaction.message.attachments) > 0:
embed.set_image(url = reaction.message.attachments[0].url)
embed.add_field(name = "Go to message", value = reaction.message.jump_url, inline = False)
await astrometrics.send(embed = embed)
You can check certain messages in on_message event. And for getting a specific channel in your server, you need to have the channel id. link
Something like this:
#bot.event
async def on_message(message):
if message.content == 'the certain message':
specific_channel = await bot.get_channel(channel_id)
await specific_channel.send(message.content)
But this might have some rate-limited consequence if the 'certain message' was too much and sent at a high rate. (If yes, then there's nothing you can do since rate-limited is a discord limitation).

Discord py delete voicechannel if its empty

Well im trying to create a voice channel by command so i can join it.
And as soon i leave the channel its supposed to get deleted.
What i tried:
if message.content.startswith('/voice'):
guild = message.guild
voicechannel = await guild.create_voice_channel(f"{str(message.author)}")
await asyncio.sleep(5)
while True:
if len(voicechannel.voice_states) == 0:
await voicechannel.delete()
break
doesnt even work tho
I believe that your issue is that you are stuck in an infinite while True loop. I would first suggest that you switch to using discord.ext.commands, it simplifies the process of creating a Discord Bot by a great deal. If you were using discord.ext.commands.Bot, you would be able to use the wait_for() function to wait until the voice channel is empty.
from discord.ext.commands import Bot
client = Bot(command_prefix="/")
#client.command(name="voice")
async def voice(ctx):
guild = ctx.guild
voice_channel = await guild.create_voice_channel(f"{str(ctx.author)}")
def check(member, before, after):
return member == ctx.author and before.channel == voice_channel and after.channel is None
await client.wait_for("voice_state_update", check=check)
await voice_channel.delete()
client.run("TOKEN")
The only issue that I think could happen, is if the member never joins the voice channel. To counter this, we can set up another wait_for() function to determine if the member ever joins before waiting for the member to leave the voice channel.
from asyncio import TimeoutError
#client.command(name="voice")
async def voice(ctx):
guild = ctx.guild
voice_channel = await guild.create_voice_channel(f"{str(ctx.author)}")
def check(member, before, after):
return member == ctx.author and before.channel is None and after.channel == voice_channel
try:
await client.wait_for("voice_state_update", check=check, timeout=60)
except TimeoutError:
await voice_channel.delete()
return

Discord bot repeating the command but not an event in discord.py

I want to ask something.I got stuck on something.I wanted to add some new features to my bot such as swear words so that making cleaner server.I finished my code and tried to give command which is !dc greetings(!dc is my command_prefix btw).My bot sent 'Hi everyone' message for 7 times.That 7 times is same number as the number of swear words in "Badwords.txt".By looking these,that repeating issue is related to this file handling codes(or not idk).Interesting thins is when I write swear word into server's chatbox,bot send only one "Please do not use this word ever" message.So How can i prevent bot not to repeat for many times?Thank you
import discord
from discord.ext import commands
intents = discord.Intents(messages=True, guilds=True, reactions=True, members=True,presences=True)
client = commands.Bot(command_prefix="!dc ", intents=intents)
#Below command gets repeated 7 times everytime.That number is same as the number of swear words in "Badwords.txt"
#client.command(aliases=["sayHi"])
async def greetings(ctx):
await ctx.send('Hi everyone')
#My purpose in the below codes is countering profanity
with open("Badwords.txt", "r", encoding="utf-8") as f:
word = f.read()
badwords = word.split(",")
#In the below event,bot sends message only 1 time for 1 message
#client.event
async def on_message(message):
msg = message.content
for x in badwords:
if x in msg:
await message.delete()
await message.channel.send("Please do not use this word ever")
else:
await client.process_commands(message)
client.run(Token)
I would take the await client.process_commands(message) out of the else statement. I have also moved the with open inside the on message event so everything a message is sent, it will open the file to check if the message contained a bad word.
import discord
from discord.ext import commands
intents = discord.Intents(messages=True, guilds=True, reactions=True, members=True,presences=True)
client = commands.Bot(command_prefix="!dc ", intents=intents)
#client.command(aliases=["sayHi"])
async def greetings(ctx):
await ctx.send('Hi everyone')
#client.event
async def on_message(message):
msg = message.content
with open("Badwords.txt", "r", encoding="utf-8") as f:
word = f.read()
badwords = word.split(",")
for x in badwords:
if x in msg:
await message.delete()
await message.channel.send("Please do not use this word ever")
else:
return
await client.process_commands(message)
client.run(Token)
await client.process_commands(message) was taken out of the else statement because, with your current code, the bot would only process the commands if the message did NOT contain a bad word. But we need it to process the commands every time a message is sent.

How to make a Discord Bot reply to a specific message rather than every message including a word

I have a bot that I want to reply to a specific word, and it does. However, if you type any message including that word, it will also reply. How do I stop that?
For example. If I was to say "Who", the bot would reply with "go away lucas"
However, if I was to say "Who is this person?" the bot would still reply with "go away lucas"
Is there any fix to this? Thanks!
async def on_message(message):
if message.content.startswith('who'):
msg = 'who'.format(message)
await message.channel.send('go away lucas')
await bot.process_commands(message)
I see you are using startswith().
the command startswith() returns True if the message starts with the desired string for example:
a = "who"
b = "who is lucas"
print(a.startswith("who"))
print(b.startswith("who"))
The output will be:
True
True
If you want the command to work only when the content of the message is "who", try this:
async def on_message(message):
if message.content == "who":
msg = 'who'.format(message)
await message.channel.send('go away lucas')
await bot.process_commands(message)

How to get my bot to join a voice channel

I'm trying to get my discord bot to connect to a voice channel like this currently:
#client.event
async def on_message(message):
message.content = message.content.lower()
if message.author == client.user:
return
if '-skip' in message.content:
await message.author.channel.connect
await message.channel.send (f"-p scotland forever")
await disconnect
Basically I want it to join a voice channel from the message author when they send the message "-skip" and then my bot joins, says -p scotland forever in chat, and then leaves. I get an error message saying things like "channel" not defined or "connect" not defined, ive tried doing it a few different ways, I think i just havent imported a plugin or whatever and thats probably my issue, but idk what plugin thing to use. Any help would be appreciated.
Try this:
#client.event
async def on_message(message):
message.content = message.content.lower()
if message.author == client.user:
return
if '-skip' in message.content:
channel = message.author.voice.channel
if channel is not None:
await channel.connect()
await message.channel.send (f"-p scotland forever")
await client.voice_clients[0].disconnect()
else:
await message.channel.send ("You need to join to voice channel")
Try this
#commands.command()
async def join_voice(self, ctx):
connected = ctx.author.voice
if connected:
await connected.channel.connect(put vc id here)
It may work
I guess?

Resources