Trying to add role once someone has joined and got the "y" confirmation from someone in the same channel
Not sure if I can add another async in the middle...
Sorry if it is a very obvious question but most answers I got online were quite outdated and the docs didn't answer all my questions
This is my attempt
import discord
from discord.ext import commands
intents = discord.Intents.default()
intents.members = True
bot = discord.ext.commands.Bot(command_prefix="$", intents=intents)
#bot.event
async def on_member_join(member):
await channel.send("{} has just joined. \nDo you know them (y/n)?".format(member.name))
print("someone joined")
async def on_message(message):
if "y" in message.content:
role = get(message.guild.roles, name="Hey i know you")
await member.add_roles(role, atomic=True)
await message.channel.send("added Hey i know you to {}".format(member.name))
One way of doing this is by using the wait_for function.
#client.event
async def on_member_join(member):
channel = client.get_channel(your channel id)
guild = client.get_guild(your guild id)
await channel.send(f"{member.mention} joined!")
await asyncio.sleep(3)
await channel.send("{} has just joined. \nDo you know them (y/n)?".format(member.name))
msg = await client.wait_for("message")
if msg.content == "y":
role = guild.get_role(your rold id)
await member.add_roles(role)
await channel.send("Added role!")
elif msg.content == "n":
await channel.send("i wont add the role!")
return
Related
It only seems like the ban command is not working here. The "hello" and "insult me" responses work and it prints out the bot name and server name when running.
Full main.py:
import os
import random
import discord
from dotenv import load_dotenv
from discord.ext import commands
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
GUILD = os.getenv('DISCORD_GUILD')
PREFIX = os.getenv('DISCORD_PREFIX')
client = discord.Client()
bot = commands.Bot(command_prefix='!',
help_command=None) # help_command to disable the default one created by this library.
#client.event
async def on_ready():
for guild in client.guilds:
if guild.name == GUILD:
break
print(
f'{client.user} is connected to the following guild:\n'
f'{guild.name}(id: {guild.id})'
)
#client.event
async def on_member_join(member):
await member.create_dm()
await member.dm_channel.send(
f'Hi {member.name}, welcome to my Discord server!'
)
#client.event
async def on_message(message):
reponse = ''
if message.author == client.user:
return
greeting = [
f"Hello there, {message.author.mention}",
f"hi!!",
'Penis enlargement pills are on their way!',
f"You're stupid, {message.author.mention}",
]
insult = [
"You're a dumb dumb!",
"You stupid meanie face!",
"Go eat a dick, you stupid mudblood!"
]
if "hello" in message.content:
response = random.choice(greeting)
await message.channel.send(response)
if 'insult me' in message.content:
response = random.choice(insult)
await message.channel.send(response)
#bans a user with a reason
#bot.command()
#commands.has_any_role("Keyblade Master","Foretellers")
async def ban (ctx, member:discord.User=None, reason =None):
if member == None or member == ctx.message.author:
await ctx.channel.send("You cannot ban yourself")
return
if reason == None:
reason = "For being a jerk!"
message = f"You have been banned from {ctx.guild.name} for {reason}"
await member.send(message)
# await ctx.guild.ban(member, reason=reason)
await ctx.channel.send(f"{member} is banned!")
client.run(TOKEN)
I believe the most relevant parts are:
client = discord.Client()
bot = commands.Bot(command_prefix='$',
help_command=None) # help_command to disable the default one created by this library.
and
#bans a user with a reason
#bot.command()
async def ban (ctx, member:discord.User=None, reason =None):
if member == None or member == ctx.message.author:
await ctx.channel.send("You cannot ban yourself")
return
if reason == None:
reason = "For being a jerk!"
message = f"You have been banned from {ctx.guild.name} for {reason}"
await member.send(message)
# await ctx.guild.ban(member, reason=reason)
await ctx.channel.send(f"{member} is banned!")
I'm sure that i'm just incorrectly implementing the bot/command in the code somewhere, but I'm not sure where.
I don't get any errors at all when I try to either run the bot or type the command in chat.
You make a client and bot but you only use client for your code. Commands registered in the bot never get called because it is not even running.
You should look at tutorials and ask for help in the discord.py Discord server
I am not sure but you need to return the reason if there is no reasons typed and reason = reason is better. So your code should look like something like this:
async def ban (ctx, member:discord.User=None, reason = reason):
if member == None or member == ctx.message.author:
await ctx.channel.send("You cannot ban yourself")
return
if reason == None:
reason = "For being a jerk!"
return
message = f"You have been banned from {ctx.guild.name} for {reason}"
await member.send(message)
# await ctx.guild.ban(member, reason=reason)
await ctx.channel.send(f"{member} is banned!")
I am trying to make my discord bot kick someone if they start typing and was wondering if it's possible. Or if a certain user sends a message at all. Was wondering if this is possible. Here is my code so far:
#Import Discord
import discord
#Client And Pemrs
client = discord.Client()
intents = discord.Intents.default()
intents.members = True
client = discord.Client(intents=intents)
#client.event
async def on_typing(channel, user, when,):
if user.id == 574638576155754498:
general_channel = client.get_channel(784127484823339031)
await kick(reason="He said the word")
#client.event
async def on_ready():
#What The Bot Doing
#client.event
async def on_message(message):
if "Valorant" in message.content:
await message.author.kick(reason="He said the word")
if "valorant" in message.content:
await message.author.kick(reason="He said the word")
if "VALORANT" in message.content:
await message.author.kick(reason="He said the word")
#Run The Client
client.run('token')
Thanks in advance for any help.
Also,
if "Valorant" in message.content:
await message.author.kick(reason="He said the word")
if "valorant" in message.content:
await message.author.kick(reason="He said the word")
if "VALORANT" in message.content:
await message.author.kick(reason="He said the word")
Can be simplefied into
if "valorant" in message.content.lower():
await message.author.kick(reason="He said the word")
This should be a comment, but since it doesn't support Markdown i posted it as an answer
First of all, you can't have events in events, so move on_message to outside of on_ready.
When you did this, the code should kick members when they say Valorant.
Then, in on_typing, simply add user before kick(reason="He said the word"), and before that, test if it is a member object:
#client.event
async def on_typing(channel, user, when,):
if user.id == 574638576155754498:
general_channel = client.get_channel(784127484823339031)
if isinstance(user, discord.Member): #when this is not true, the typing occured in a dm channel, where you can't kick.
await user.kick(reason="He said the word")
So your final code would be:
#Import Discord
import discord
#Client And Pemrs
client = discord.Client()
intents = discord.Intents.default()
intents.members = True
client = discord.Client(intents=intents)
#client.event
async def on_typing(channel, user, when,):
if user.id == 574638576155754498:
general_channel = client.get_channel(784127484823339031)
if isinstance(user, discord.Member): #when this is not true, the typing occured in a dm channel, where you can't kick.
await user.kick(reason="He said the word")
#client.event
async def on_ready():
print("Client is ready") #the on_ready function is called only when the client is ready, so we just print in there that the client is ready
#client.event
async def on_message(message):
if "valorant" in message.content.lower(): #use the changes dank tagg suggested
await message.author.kick(reason="He said the word")
#Run The Client
client.run('token')
References:
member.kick
so, Ive been doing this:
from discord.ext import commands
from discord.utils import get
client = commands.Bot(command_prefix='><')
#client.event
async def on_ready():
print("I am ready Winson or not Winson :D")
#client.event
async def on_member_join(member):
channel = client.get_channel(744440768667844698)
message = await channel.send(f"Welcome to HaveNoFaith {member}, happy to be friends with you")
#client.command()
async def ping(ctx):
await ctx.send(f"Your Ping is {round(client.latency *1000)}ms")
#client.command()
async def Help(ctx2):
await ctx2.send("Hi, Im WelcomeBot v1.0...\n\nPrefix: ><\n\nCommands: ping\n help")
#and then Im trying to do like at the message "Welcome etc" if they react with the "check reaction" in that message, they will get a role in the discord server...
you can make a command named addrr(add reaction role), which will look like this -
#client.command()
#commands.guild_only()
#commands.has_permissions(administrator=True)
async def addrr(self, ctx, channel: discord.TextChannel, message: discord.Message, emoji: discord.Emoji,
role: discord.Role):
await ctx.send(f"Setting up the reaction roles in {channel.mention}.")
await message.add_reaction(emoji)
def check1(reaction, user):
return user.id is not self.client.user.id and str(reaction.emoji) in [f"{emoji}"]
while True:
try:
reaction, user = await self.client.wait_for("reaction_add", check=check1)
if str(reaction.emoji) == f"{emoji}":
await user.add_roles(role)
await message.remove_reaction(reaction, user)
else:
await message.remove_reaction(reaction, user)
except:
await message.delete()
break
It will work like this -
><addrr <#channel mention> <message ID> <Emoji> <#Role Mention>
So you can add a reaction to the messages that was sended and use wait_for to wait for a reaction on that message. I recommend you to add the timeout. If you dont want to have this timeout, simply just send these message, save it into a list and in the raw_reaction_add event check if the emoji is the one and the message is one of the messages in your list
so I'm working on making my own bot for my server and after a while I finally found a string for autorole & role assignment that worked.
I then kept on adding another string for the bot simply replying "Hello". As soon as I add that the role commands won't work anymore. Once I take it out it works again.
On the other hand I have a 8ball and a dice roll command that works with and without the Hello Command
I have no idea what is the problem...
#client.event
async def on_member_join(member):
channel = discord.utils.get(member.guild.channels, name='entrance')
await channel.send(f'Welcome {member.mention} to Dreamy Castle! \n Please make sure to read the rules!')
role = discord.utils.get(member.guild.roles, name="Peasants")
await member.add_roles(role)
#client.event
async def on_message(message):
if message.content.startswith('+acceptrules'):
member = message.author
role1 = discord.utils.get(member.guild.roles, name='The People')
await member.add_roles(role1)
#client.event #this is the hello command
async def on_message(message):
message.content.lower()
if message.content.startswith('Hello Conny'):
await message.channel.send('Hello!')
Use if and elif not 2 different functions for same event.
Also you might need commands.Bot for a fully functional commanded bot.
#client.event
async def on_message(message):
if message.content.startswith('+acceptrules'):
member = message.author
role1 = discord.utils.get(member.guild.roles, name='The People')
await member.add_roles(role1)
elif message.content.lower().startswith("hello conny"):
await message.channel.send("Hello!")
await client.process_commands(message)
ok so here is my code
import discord
from discord.ext import commands
TOKEN = 'THIS_IS_MY_BOT_TOKEN'
client = commands.Bot(command_prefix = '.')
#client.event
async def on_message(message):
# we do not want the bot to reply to itself
if message.author == client.user:
return
#typing cat
if message.content.startswith('!cat'):
msg = 'https://media.giphy.com/media/JIX9t2j0ZTN9S/giphy.gif'.format(message)
await client.send_message(message.channel, msg)
#I dont need sleep i need awnsers
if message.content.startswith('!sleep'):
msg = 'https://i.kym-cdn.com/entries/icons/original/000/030/338/New.jpg'.format(message)
await client.send_message(message.channel, msg)
#murica
if message.content.startswith('!murica'):
msg = 'https://www.dictionary.com/e/wp-content/uploads/2018/08/Murica_1000x700.jpg'.format(message)
await client.send_message(message.channel, msg)
if message.content.startswith('!why'):
msg = 'https://drive.google.com/file/d/1rb132Y785zUjj2RP2G-a_yXBcNK5Ut9z/view?usp=sharing'.format(message)
await client.send_message(message.channel, msg)
#client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
#client.command(pass_context=True)
async def join(ctx):
if ctx.message.author.voice:
channel = ctx.message.author.voice.channel
await channel.connect()
client.run(TOKEN)
the bot joins the sever, but when I say .join, nothing happens
the voice channel I want to join is called Club Meeting if that helps
Not entirely sure why, I have no errors when I run it. Anyone have any idea whats going on?
I think the problem is your lack of Bot.process_commands You need to put that at the end of your on_message function. That seems to be the reason your command isn't working.
From The Docs:
Why does on_message make my commands stop working?
https://discordpy.readthedocs.io/en/latest/faq.html#why-does-on-message-make-my-commands-stop-working