I want to create a promote command which when issued it promotes the member to the next rank, but I can't get it to remove/add his roles
import discord
import os
from discord.utils import get
bot = discord.Bot(intents=discord.Intents.all())
admin = 990420666568278086
#roles
prospect = 869470046953537546
member = 869470100061814784
patched = 1003733917704134737
#bot.event
async def on_ready():
print(f"{bot.user} is ready and online!")
#bot.slash_command(name="promote", description = "Promote people to the next rank")
async def promote(ctx, user: discord.Member):
if ctx.author.get_role(admin):
if user.get_role(prospect):
await user.remove_roles(prospect)
await user.add_roles(member, patched)
bot.run("DISCORD-TOKEN")
Please refering to discord.Member.add_roles and discord.Member.remove_roles. Both of them are taking discord.Role as parameter. Hence your code should look like user.remove_roles(guild.get_role(prospect)) where guild.get_role(prospect) is returning discord.Role
This is what your code should looks like to make it works. Happy coding!
async def promote(ctx, user: discord.Member):
guild=ctx.guild
if ctx.author.get_role(admin):
if user.get_role(prospect):
await user.remove_roles(guild.get_role(prospect))
await user.add_roles(guild.get_role(member), guild.get_role(patched))
Related
So I'm putting this in a Cog. I want to make it so that if the author is a specific person and they type anything, the bot will mention them and reply.
import discord
from discord.ext import commands
client = discord.Client()
class jtieu(commands.Cog):
def __init__(self, client):
self.client = client
#commands.Cog.listener()
async def on_ready(self):
print("Bot 3 is ready.")
#commands.Cog.listener()
async def jtieu2(self, ctx):
if ctx.author == "CxS#3441" :
await ctx.channel.send(f"{ctx.author.mention} ok")
def setup(client):
client.add_cog(jtieu(client))
I'm not too sure if I'm supposed to use ctx.author.mention in this context, and I'm fairly new to how Cogs work in discord.py.
If you are creating a mention specifically for one user it might be a better idea to copy the user's id and use it instead of a nickname as it will work even when they change their name.
#commands.Cog.listener()
async def on_message(self, message):
if message.author.id == 1234567890: # example id
await message.channel.send(f"{message.author.mention} ok")
Remember that it requires intents.messages.
Simple Cogs Example
i've encountered an issue where I can't seem to get my addrole command to work. I've searched through every video and article and have spent hours to try and figure out why my code isn't working.
I want it so whenever a admin calls the ,addrole command, the user they mentioned gets that role.
(example: ,addrole #{rolename} {#user}).
Here is 2 sections of the code which I think may be the issue.
Here is the imports and some other things.
from discord.ext import commands
import random
from discord import Intents
import os
from discord import Embed
from discord.ext.commands import has_permissions
from discord.utils import get
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv("TOKEN")
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix=",", intents=intents)
Here is the command.
#commands.has_permissions(administrator=True)
async def addrole(ctx, role: discord.Role, user: discord.Member):
await user.add_roles(role)
await ctx.send(f"{user.mention} has successfully received the role {role.mention}.")
I've tried changing so many things but whenever I run it and call the command nothing happens.
Thanks.
Your command is not responding as it is missing client.command() which actually calls the command
#client.command()
#commands.has_permissions(administrator=True)
async def addrole(ctx, role: discord.Role, user: discord.Member):
await user.add_roles(role)
await ctx.send(f"{user.mention} has successfully received the role {role.mention}.")
It may also be better if your allow the role to have multiple arguments, which means you can add a role to a user with spaces, for example, This role instead of Thisrole, this can be added by adding * which creates an multiple string argument. You would then need to pass it as the last argument.
#client.command()
#commands.has_permissions(administrator=True)
async def addrole(ctx, user: discord.Member=None, *, role: discord.Role):
if user == None:
await ctx.send('Please format like ,addrole #member role')
return
await user.add_roles(role)
await ctx.send(f"{user.mention} has successfully received the role {role.mention}.")
I was wondering how I could make a command that kicks a specific user every time, without having to mention the user.
I am looking for something like this:
#client.command()
async def kick(ctx):
user = #user id
await user.kick(reason=None)
And not like this:
#client.command()
async def kick(ctx, member : discord.Member, *, reason=None):
await member.kick(reason=reason)
Thanks in advance
You can get a member from his/her id by using discord.Guild.get_member.
#client.command()
async def kick(ctx):
user = ctx.guild.get_member(<user id>)
await user.kick(reason=None)
Or you can use discord.utils.get. I'd recommend you to use the first one but this is also a option.
#client.command()
async def kick(ctx):
user = discord.utils.get(ctx.guild.members, id=<user id>)
await user.kick(reason=None)
References
discord.Guild.get_member()
discord.utils.get()
you can use the built in converter for member : discord.Member
like this:
from discord.ext import commands
#client.command()
async def kick(ctx, member: commands.MemberConverter, *, reason):
await ctx.guild.kick(member, reason=reason)
correct me if i'm wrong but it should work
you can watch this video if you're confused: https://www.youtube.com/watch?v=MX29zp9RKlA (this is where i learned all of my discord.py coding stuff)
import discord
import os
flist = []
client = discord.Client()
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('?ID'):
for i in ID.split()[1].split('#'):
flist.append(i)
print(flist)
print(discord.utils.get(client.get_all_members(), name = flist[0], discriminator = flist[1]).id)
client.run(TOKEN)
I want to have a bot get the id of a use just by entering the the name and the hashtag number but it returns an error that the final line has nonetype. How could I fix this so it shows the id as a string?
First of all let me recommend you to use the discord.ext.commands extension for discord.py, it will greatly help you to make commands.
You just need to get a discord.Member or discord.User in this case. Since these classes have built in converters, you can use them as typehints to create an instance:
from discord.ext import commands
bot = commands.Bot(command_prefix='?', ...)
#bot.command()
async def showid(ctx, member: discord.Member): # a Union could be used for extra 'coverage'
await ctx.send(f'ID: {member.id}')
Using a typehint in this case allows the command user to invoke the command like:
?showid <id>, ?showid <mention>, ?showid <name#tag>, ?showid <nickname> etc with the same result.
If you don't want to use discord.ext.commands, you can use discord.utils.get as well:
client = discord.Client(...)
#client.event
async def on_message(message):
...
if message.content.startswith('?ID'):
member = discord.utils.get(message.guild.members, name=message.content.split()[1])
await message.channel.send(member.id)
...
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.