Verify Command with discord.py rewrite - python

Im trying to make a verify command where it sends me a dm, and I can check wether they are verified or not, here is my code:
import discord
from discord.ext import commands, tasks
from itertools import cycle
import os
import time
import json
token = os.environ['token']
def get_prefix(client, message):
with open('prefixes.json', 'r') as f:
prefixes = json.load(f)
return prefixes[str(message.guild.id)]
client = commands.Bot(command_prefix=get_prefix)
#client.event
async def on_ready():
print('Bot is ready')
await client.change_presence(activity=discord.Game(f'My prefix is {get_prefix}'))
#client.command()
async def verify(ctx, message, jj=discord.Member.get_user("270397954773352469")):
person = message.author
jj.create_dm()
await ctx.send('Awaiting Verification, you will recieve a dm when you are verified')
await jj.send(f'{person} is trying to be verified, do you know him/her?')

You don't have to use parameter message use ctx which you already have to get ctx.author. You can get a user by id only with client.get_user or client.fetch_user (and use it inside a function, not as a parameter). Last thing - you usually don't have to use create_dm(), but if you want to you can, but remember to correct it to await jj.create_dm().
#client.command()
async def verify(ctx):
jj = await client.fetch_user(270397954773352469)
person = ctx.author
await ctx.send('Awaiting Verification, you will recieve a dm when you are verified.')
await jj.send(f'{person.mention} is trying to be verified, do you know him/her?')
If you have something like verified role (you won't get a message if someone is verified):
#client.command()
async def verify(ctx):
role = discord.utils.get(ctx.guild.roles, name="verified") # name of your role
person = ctx.author
if role in person.roles:
await ctx.send('You are already verified!')
else:
jj = await client.fetch_user(270397954773352469)
await ctx.send('Awaiting Verification, you will recieve a dm when you are verified.')
await jj.send(f'{person.mention} is trying to be verified, do you know him/her?')

Related

How do I give roles to a person that reacted to my message?

In the code below I am trying to give a role to a person whenever they react to my message but this code throws me an error saying that 'int' object has no attribute 'id' the code says that the problem is with this code: await user.add_roles(user.guild.id, user.id, role, reason='reason') how do I solve this issue?
import discord
from discord.ext import commands
import random
client = discord.Client()
#client.event
async def on_ready():
print('ready')
#client.event
async def on_reaction_add(reaction, user):
channel = reaction.message.channel
await channel.send(f'{user.name} has reacted by using {reaction.emoji} emoji, his message was {reaction.message.content}')
role = discord.utils.get(user.guild.roles, name = 'Test_Bot')
await user.add_roles(user.guild.id, user.id, role, reason='reason')
client.run('TOKEN')
As Dominik said, you do not need user.guild.id, user.id if you say await user.add_roles. What is happening is that it is trying to use the user.id (an integer) you passed instead of the user object you use when calling the function to begin with: await user.add_roles(...).
So, get rid of await user.add_roles(user.guild.id, user.id, role, reason='reason'), and make it just: await user.add_roles(role).
Fixed code should be:
import discord
from discord.ext import commands
import random
client = discord.Client()
#client.event
async def on_ready():
print('ready')
#client.event
async def on_reaction_add(reaction, user):
channel = reaction.message.channel
await channel.send(f'{user.name} has reacted by using {reaction.emoji} emoji, his message was {reaction.message.content}')
role = discord.utils.get(user.guild.roles, name = 'Test_Bot')
await user.add_roles(role, reason='reason')
client.run('TOKEN')
TL;DR: Discord.py expects the full member object when you use add_roles(). You give it the integer of the member's ID instead.

Verification using Python Discord

I'm making a bot with python and I need help with two things.
Making a welcome message for users that include mentioning the user and mentioning the channel
Making a command that will remove the role "Unverified" and add 4 other roles. I also need it to send a message in the verification channel to make sure the person has been verified and send an embed in general chat telling the user to get self roles.
Well you could try
import discord
from discord.ext import commands
from discord.utils import get
client = commands.Bot(command_prefix=".")
confirmEmoji = '\U00002705'
#client.event()
async def on_ready():
print("[Status] Ready")
#client.event()
async def on_member_join(ctx, member):
channel = get(ctx.guild.channels,name="Welcome")
await channel.send(f"{member.mention} has joined")
#client.command()
async def ConfirmMessage(ctx):
global confirmEmoji
message = await ctx.send("Confirm")
await message.add_reaction(emoji=confirmEmoji)
def check(reaction, user):
if reaction.emoji == confirmEmoji:
return True
else:
return False
while True:
try:
reaction, user = await client.wait_for("reaction_add", check=check, timeout=10)
roleToRemove = get(ctx.guild.roles,name="unverified")
memberToRemoveRole = get(ctx.guild.members,name=user.display_name)
await memberToRemoveRole.remove_roles(roleToRemove)
Now all you have to do is go to the channel and enter .ConfirmMessage

Discord.py on_message() but only for private messages

So, I'm working on a discord bot. And am using the on_message() event, which works both on private messages and on servers. I want this to only work in private messages and am unsure of how to go about this.
If anyone can help me, that would be great.
import os
import discord
from discord.ext import commands
TOKEN = ''
quotedUsers = []
client = commands.Bot(command_prefix = '/')
#client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
await client.change_presence(activity=discord.Game(name='with myself.'))
#client.event
async def on_message(message):
await message.author.send("Recieved: " + message.content)
#client.command()
async def search(ctx, *, question):
await ctx.author.send("Searching: " + question)
client.run(TOKEN)
A message guild also doesn't exist in a group DM, so you have to check if the channel you messaging in is a DM. You can use the dm_channel attribute of a user:
#client.event
async def on_message(message):
if message.channel.id == message.author.dm_channel.id: # dm only
# do stuff here #
elif not message.guild: # group dm only
# do stuff here #
else: # server text channel
# do stuff here #
When a DM is received, it won't have a guild, so you'll be able to use that logic like so:
#client.event
async def on_message(message):
# you'll need this because you're also using cmd decorators
await client.process_commands(message)
if not message.guild:
await message.author.send(f"Received: {message.content}")
References:
Bot.process_commands()
Message.guild - it mentions "if applicable", meaning that it'll return None if it's not in a guild, and rather a DM
f-Strings - Python 3.6+
Hi i had found though playing around that when discord sends a message to the bot as a dm in python this will be done using a discord.channel.DMChanneland if the message is done in the text channel discord.channel.TextChannel.
i have proved this by putting in a type fuct over the object to see what happens in the Message listener.
isinstance(message.channel, DMChannel)

How to send private message to member in on_member_join() discord.py?

This is what I have:
#client.command(pass_context=True)
#client.event
async def on_member_join(ctx, member):
print(f'{member} has joined a server.')
await ctx.send(f"Hello {member}!")
await ctx.member.send(f"Welcome to the server!")
I need the bot to send a private message containing rules and commands list when he joins.
Please help!
The event on_member_join() only accepts member as a valid parameter (see doc). Thus what you try to do: on_member_join(ctx, member) ,wont work. You need to use this instead: on_member_join(member).
If you used the event as follows:
#client.event
async def on_member_join(member):
await member.send('Private message')
You can send messages directly to members who joined the server. Because you get an member object using this event.
I don't know what happened, from one day to the next the bot stopped sending welcome messages to new members. But I was finally able to solve it.
I just had to add these two lines of code. intents = discord.Intents() intents.members = True Read
import discord
from discord.ext import commands
#try add this
intents=discord.Intents.all()
#if the above don't work, try with this
#intents = discord.Intents()
#intents.members = True
TOKEN = 'your token'
bot=commands.Bot(command_prefix='!',intents=intents)
#Events
#bot.event
async def on_member_join(member):
await member.send('Private message')
#bot.event
async def on_ready():
print('My bot is ready')
bot.run(TOKEN)

Discord Python Bot: How to move specific users mentioned by the author of the message

I am looking for a way to allow a user to move him or her self and another user to a different voice channel. I already got the command to work for the author of the message, but I am having trouble finding out a way to move another user in the same message. The idea is that the user would be able to type "n!negotiate [Other User]" and it would move the author and the other user to the Negotiation channel.
I would love some help with how I might be able to do this. The code is provided below excluding the tokens and ids.
Code:
import discord
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio
import time
Client = discord.Client() #Initialise Client
client = commands.Bot(command_prefix = "n!") #Initialise client bot and prefix
#client.event
async def on_ready():
print("Logged in as:")
print(client.user.name)
print("ID:")
print(client.user.id)
print("Ready to use!")
#client.event
async def on_message(check): #Bot verification command.
if check.author == client.user:
return
elif check.content.startswith("n!check"):
await client.send_message(check.channel, "Nations Bot is online and well!")
async def on_message(negotiation): #Negotiate command. Allows users to move themselves and other users to the Negotiation voice channel.
if negotiation.author == client.user:
return
elif negotiation.content.startswith("n!negotiate"):
author = negotiation.author
voice_channel = client.get_channel('CHANNELID')
await client.move_member(author, voice_channel)
client.run("TOKEN")
You should use discord.ext.commands. You're importing it, but not actually using any of the features.
from discord.ext import commands
import discord
bot = commands.Bot(command_prefix = "n!") #Initialize bot with prefix
#bot.command(pass_context=True)
async def check(ctx):
await bot.say("Nations Bot is online and well!")
#bot.command(pass_context=True)
async def negotiate(ctx, member: discord.Member):
voice_channel = bot.get_channel('channel_id')
author = ctx.message.author
await bot.move_member(author, voice_channel)
await bot.move_member(member, voice_channel)
bot.run('TOKEN')
Here we use a converter to accept a Member as input. Then we resolve the author of the message from the invocation context and move both Members to the voice channel.

Categories

Resources