AutoMod not reading correct word - discord

I am attempting to add an automod cog to my discord bot. I have written a blacklist command and whitelist command in a different file and they work fine. What i can't figure out is when it loads the blacklisted words from the .csv file it loads them to the variable as ["['Test']"] and not as Test. If anyone knows how to fix this please let me know.
class AutoMod(commands.Cog):
def __init__ (self, bot):
self.bot = bot
self.words = Words
#commands.Cog.listener()
async def on_ready(self):
self.words = {}
with open(Wdir, 'r') as csv_file:
csvreader=csv.reader(csv_file)
for line in csvreader:
Words.append(str(line))
print(f'AutoMod {Words}')
#commands.Cog.listener()
async def on_message(self, message):
print(Words)
if str(Words) in message.content:
await message.delete()
await message.channel.send(f'{message.author.mention}, You are not allowed to say that')
else:
pass
def setup(bot):
bot.add_cog(AutoMod(bot))

You can do smth like this:
filtered_words = ["badword", "verybadword"]
#client.event
async def on_message(message):
for word in filtered_words:
if word in message.content:
await message.delete()
await message.channel.send("This word is blacklisted")
await client.process_commands(message)
Now bot will delete a message that contains any of filtered words. Bot will also send a message that says word is blacklisted.

Related

Making my discord bot listen for specific messages and answer back when it identifies one

So, I'm new to python and even more to coding bots on discord. At the moment I'm using discord.py and red-bot (https://docs.discord.red/en/stable/index.html).
At the moment I'm trying to make the bot listen to a new message and print something in response to it, but I just can't figure it out. Since I'm using red-bot I didn't go through the steps of using client = discord.Client() and setting a token on the code itself so using #client.event() doesn't seem to work, nor #bot.event(), and I can't really find any other way to make the bot listen for an on_message() event.
Edit with part of my code:
import discord
from discord.ext import commands
from redbot.core import commands
class MyCog(commands.Cog):
client = discord.Client()
def __init__(self, bot):
self.bot = bot
#bot.event()
async def on_message(self, message):
if 'test' in message.content:
await self.send_message(message.channel, 'it works!')
Console returns that bot in #bot.event() is not defined.
Also a separate initialization file, it's done this way to follow Red's guide of how to make a cog.
from .remind import MyCog
def setup(bot):
bot.add_cog(MyCog(bot))
Here is an example of a cog that replies to a specific word mentioned. It also has a cooldown to prevent the bot spamming which I find absolutely neccesary to include.
from curses.panel import bottom_panel
import discord
from discord.ext import commands
class brilliant(commands.Cog):
def __init__(self, bot):
self.bot = bot
self._cd = commands.CooldownMapping.from_cooldown(1, 60.0, commands.BucketType.member) # Change accordingly
# rate, per, BucketType
def ratelimit_check(self, message):
"""Returns the ratelimit left"""
bucket = self._cd.get_bucket(message)
return bucket.update_rate_limit()
#commands.Cog.listener()
async def on_message(self, message):
if message.author == self.bot.user:
return
msg = message.content.lower()
brilliant = ['brilliant', 'Brilliant', 'brilliant!', 'Brilliant!']
if any(word in msg for word in brilliant):
retry_after = self.ratelimit_check(message)
if retry_after is None:
await message.channel.send("Brilliant!")
await self.bot.process_commands(message)
else:
return
async def setup(bot):
await bot.add_cog(brilliant(bot))
If you want it in the main file, you can put something a lot simpler:
brilliant = ['brilliant', 'Brilliant', 'brilliant!', 'Brilliant!']
#bot.event
async def on_message(message):
if message.author == bot.user:
return
msg = message.content.lower()
if any(word in msg for word in brilliant):
await message.channel.send("Brilliant!")
await bot.process_commands(message)
Hope it helps.

How can I have a trigger without my command prefix, like welcome and not $welcome?

So I wanted to make a feature on my discord bot and I wanted it to respond when users say welcome and say Welcome! back. But every time I try it only responds to $welcome. ($ is my prefix). What should I do?
client = commands.Bot(command_prefix = "$")
#client.command()
async def welcome(ctx, message):
if any(word in message for word in welcome):
await message.channel.send("Welcome!")
#client.event
async def on_message(message):
if "welcome" in message.content:
await message.channel.send('Welcome!')

In discord.py when I use more than 1 `on_message` it does not works, only the last one works

This is my code, and on_message is not working when used twice, only the 2nd one is working. Please help me.
async def on_message(message):<br>
if message.content.startswith('-coinflip'):<br>
embedVar = discord.Embed(<br>
title="Toss",<br>
description=(f'You got {random.choice(heads_tails)}'),<br>
color=(0xFF0000))<br>
print(f'-coinflip command used by {message.author}')<br>
await message.channel.send(embed=embedVar)<br>
#client.event<br>
async def on_message(message):<br>
if message.content.startswith('-help'):<br>
embedVar = discord.Embed(<br>
title="Help arrived!",<br>
description="So, it looks like you need help, let me help you",<br>
colour=0xFF0000)<br>
embedVar.add_field(name="Bot Prefix", value="-", inline=False)<br>
embedVar.add_field(name="Moderation Commands",<br>
value="-help",<br>
inline=True)<br>
embedVar.add_field(name="Fun commands", value="-coinflip", inline=True)<br>
embedVar.set_thumbnail(<br>
url=<br>
"https://media.discordapp.net/attachments/923531605660815373/974248483479494686/charizard-mega-charizard-y.gif"<br>
)<br>
print(f'-help command used by {message.author}')<br>
await message.channel.send(embed=embedVar)<br>```
Here's the answer I wrote but couldn't post:
You cannot have 2 on_message event listeners. You can merge the two event listeners and their responses by using if...elif like this instead:
#bot.event
async def on_message(message): #When any message is sent
if message.content.startswith('-coinflip'):
embedVar = discord.Embed(
title="Toss",
description=f'You got {random.choice(heads_tails)}',
color=(0xFF0000))
print(f'-coinflip command used by {message.author}')
await message.channel.send(embed=embedVar)
elif message.content.startswith('-help'):
embedVar = discord.Embed(
title="Help arrived!",
description="So, it looks like you need help, let me help you",
colour=0xFF0000)
embedVar.add_field(name="Bot Prefix", value="-", inline=False)
embedVar.add_field(name="Moderation Commands",
value="-help",
inline=True)
embedVar.add_field(name="Fun commands", value="-coinflip", inline=True)
embedVar.set_thumbnail(
url="https://media.discordapp.net/attachments/923531605660815373/974248483479494686/charizard-mega-charizard-y.gif")
print(f'-help command used by {message.author}')
await message.channel.send(embed=embedVar)
elif is the same as else if; in this case, if the message's content doesn't start with "-coinflip" and starts with "-help", it creates and sends an Embed.
I've replaced the heads_tails variable for a fully-functioning code:
from discord.ext import commands
import discord
import discord.utils
import random
intent = discord.Intents(messages=True, message_content=True, guilds=True)
bot = commands.Bot(command_prefix="", description="", intents=intent)
heads_tails = ("Heads", "Tails") #Replace this with your sequence
#bot.event
async def on_ready(): #When the bot comes online
print("It's online.")
#bot.event
async def on_message(message): #When any message is sent
if message.content.startswith('-coinflip'):
embedVar = discord.Embed(
title="Toss",
description=f'You got {random.choice(heads_tails)}',
color=(0xFF0000))
print(f'-coinflip command used by {message.author}')
await message.channel.send(embed=embedVar)
elif message.content.startswith('-help'):
embedVar = discord.Embed(
title="Help arrived!",
description="So, it looks like you need help, let me help you",
colour=0xFF0000)
embedVar.add_field(name="Bot Prefix", value="-", inline=False)
embedVar.add_field(name="Moderation Commands",
value="-help",
inline=True)
embedVar.add_field(name="Fun commands", value="-coinflip", inline=True)
embedVar.set_thumbnail(
url="https://media.discordapp.net/attachments/923531605660815373/974248483479494686/charizard-mega-charizard-y.gif")
print(f'-help command used by {message.author}')
await message.channel.send(embed=embedVar)
Also, is "-help" a moderation command? And, try searching for and solving such problems yourself. StackOverflow shouldn't be the first place to ask such questions.

Discord.py bot react to user's next message after using a specific command

For example:
I use !react command and then after 1 min I send a message and then the bot reacts to the message with a specific emoji. Is it possible?
#commands.command()
#commands.cooldown(1, 600, commands.BucketType.user)
async def cookieme(self, ctx):
...somehow remembers to this name and next time the user say something it reacts to the msg with a cookie
I don't really understand your question but I will try to help you:
#bot.command()
async def react(ctx):
def check(message):
return ctx.author == message.author and ctx.channel == message.channel
react = await bot.wait_for("message", check=check)
await react.add_reaction("🍪")

How can I make my bot say full sentences that I say?

This is the code I am using currently:
#client.command()
async def say(ctx, arg: str):
await ctx.channel.purge(limit=1)
await ctx.send(f"{arg}")
This code works, but the bot only says the first word of the sentence. How can I make it say the whole sentence?
I hope this would work:
#client.command()
async def say(ctx, *args):
await ctx.message.delete()
# do you want to say it with TTS ?
await ctx.send(' '.join(args), tts=True)

Resources