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
Related
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?')
I would just like to send a DM to my friend via python code.
This is my code, but it does not work.
Code:
import discord
client = discord.Client(token="MY_TOKEN")
async def sendDm():
user = client.get_user("USER_ID")
await user.send("Hello there!")
Your bot might now have the user in its cache. Then use fetch_user
instead of get_user (fetch requests the Discord API instead of
its internal cache):
async def sendDm():
user = await client.fetch_user("USER_ID")
await user.send("Hello there!")
You can run it with on_ready event:
#client.event
async def on_ready():
user = await client.fetch_user("USER_ID")
await user.send("Hello there!")
Copy and paste:
import discord
client = discord.Client()
#client.event
async def on_ready():
user = await client.fetch_user("USER_ID")
await user.send("Hello there!")
client.run("MY_TOKEN")
So are you trying to do this with a command? If so here is that
#bot.command()
async def dm(ctx, user: discord.User = None, *, value = None):
if user == ctx.message.author:
await ctx.send("You can't DM yourself goofy")
else:
await ctx.message.delete()
if user == None:
await ctx.send(f'**{ctx.message.author},** Please mention somebody to DM.')
else:
if value == None:
await ctx.send(f'**{ctx.message.author},** Please send a message to DM.')
else:
` await user.send(value)
I'm trying to make a bot that will write to the chat what the user is playing, but even when the game is running, None is displayed all the time
What am I doing wrong?
Working code:
from discord.ext import tasks
import discord
intents = discord.Intents.all()
intents.presences = True
class MyClient(discord.Client):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
async def on_ready(self):
print('Logged in as')
print(self.user.name)
print(self.user.id)
print('------')
#tasks.loop(seconds=5)
async def activity_task(self, message):
mentions = message.mentions
if len(mentions) == 0:
await message.reply("Remember to give someone to get status!")
else:
activ = mentions[0].activity
if activ is None:
await message.reply("None")
else:
await message.reply(activ.name)
#activity_task.before_loop
async def before_my_task(self):
await self.wait_until_ready()
async def on_message(self, message):
if message.content.startswith('!status'):
self.activity_task.start(message)
client = MyClient(intents=intents)
client.run('token')
As Ceres said, you need to allow intents.
Go to your developer's page https://discord.com/developers/applications, and go to the bot. Scroll down a bit, and you'll see this:
Turn on presence and server members intent.
Now, in your code, you'll have to add this in the beginning:
intents = discord.Intents.all()
Change your bot startup code to this
client = MyClient(intents=intents)
Now, with the intents, you want the OTHER person's activity.
So, in the activity_task method, you can't use message.author, as that will return whoever sent the message, not who you're mentioning.
Change it to this:
async def activity_task(self, message):
mentions = message.mentions
if len(mentions) == 0:
await message.reply("Remember to give someone to get status!")
else:
activ = mentions[0].activity
if activ == None:
await messag.reply("None")
else:
await message.reply(activ.name)
Now, if you do !status #[valid ping here], it should return whatever they're doing. Must note: it MUST be a valid ping.
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
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.