I'm pretty new to coding in Python and I'm starting to make a Discord bot. I've made a command that will toggle on and off commands but I can't seem to figure out how to make it server specific. Meaning if the admin of a server with my bot toggles off a command it will only toggle off on their server and not everyone else's. Any help would be great thanks.
My Code:
#client.command()
#commands.is_owner()
async def toggle(ctx, *, command):
command = client.get_command(command)
if command == None:
elk = discord.Embed(color=discord.Color.dark_gold(), title=f"I can't find a command with that name!")
await ctx.send(embed=elk)
elif ctx.command == command:
psos = discord.Embed(color=discord.Color.dark_gold(), title=f"You cannot disable this command!")
await ctx.send(embed=psos)
else:
command.enabled = not command.enabled
ternary = "enabled" if command.enabled else "disabled"
lllj = discord.Embed(color=discord.Color.dark_gold(), title=f"I have {ternary} {command.qualified.name} for you!")
await ctx.send(embed=lllj)
According to the Discord documentation about registering a command:
Commands can be scoped either globally or to a specific guild. Global commands are available for every guild that adds your app.
A guild is defined by the documentation as:
Guilds in Discord represent an isolated collection of users and channels, and are often referred to as "servers" in the UI.
I'm not sure this fully answer your question, if this is not the case I suggest you an in depth reading of the official developer documentation.
Related
I'm trying to create a music bot using nextcord slash commands and interactions. The command isn't fully finished yet, as I am having trouble getting it to even join the voice channel. I don't exactly know how interactions work yet but I'm assuming it's a similar concept as ctx. Below is my music.py cog:
import nextcord
from nextcord.ext import commands
from nextcord import Interaction
class Music(commands.Cog):
def __init__(self, client):
self.client = client
guild_ids = ["Guild Ids Go Here"]
#slash commands go under here
#nextcord.slash_command(name="play", description="plays music in vc", guild_ids = guild_ids)
async def play(self, interaction : Interaction, query: str):
channel = interaction.author.voice.channel #ERROR IS HERE
try:
await channel.connect()
await interaction.response.send_message("The bot has joined vc.")
except:
await interaction.response.send_message("Failed to find voice channel.")
def setup(client):
client.add_cog(Music(client))
I'm getting an error that says "'Interaction' object has no attribute 'author'. It occurs on line 15 in 'play' when it says 'channel = interaction.author.voice.channel'. I think this means that this isn't the right way to go about getting the author's voice channel. If this is the case, is there a better, working method?
In nextcord interaction, message author is interaction.user,
channel is interaction.channel.
You also can send interaction message by interaction.send instead
of interaction.response.send_message. It's much shorter and easier
to read.
If you want to send a normal message without using interaction, try
interaction.channel.send. It's similar to ctx.channel.send in application command
I've got an issue but I am getting no errors and from the code I just wrote as shown below
#client.commands
async def hello():
channel = int(797915093954199565)
await channel.send('Hey what are you doing?')
I am trying to make a command where the user can talk to the bot, and it responds back with something, its just a starting command but I'm having trouble with these small things, the rest of the bot works but its just this issue i'm having please help!
So assuming the rest of your bot and code works, your "hello" command isn't working because you have
#client.commands #its client.command in this case, as it seems you are not using any cogs, and you must call the client with () these double parentheses
async def hello(): #Here you have not passed ctx
channel = int(793744805615632394) #It seems what you would like to do here is send it to a specific channel, however, in the code below, I have set it so it just sends to the channel it was used in. The correct use is client.get_channel(793744805615632394)
await channel.send('Hey what are you doing?')
Here is the command but fixed:
So assuming the rest of your bot and code works, your "hello" command isn't working because you have
#client.command()
async def hello(ctx):
await ctx.send('Hey what are you doing?')
You should also refer to the py docs to learn more about it: https://discordpy.readthedocs.io/en/latest/
I'm working on a bot to manage roles for a specific server, but can't figure out how to give one role and remove another. I'm pretty new to coding, so I've been searching over the web for the correct syntax, but cannot find an answer. My code is a bit insufficient because once again, I recently started code.
#bot.command()
#commands.has_any_role("Franchise Owner", "General Manager", "Head Coach")
async def sign(ctx, member = discord.Member):
embed = discord.Embed()
[
embed.add_field(name="<:Green:786300903065518090> Transaction Complete", value="Thanks, still being worked on. It would be useful if you refrained from using the command. However, if this had been finished, it would say <team_emoji> (ex. <:DallasCowboys:786222692613226506> has signed <user>.) Other commands will be worked on soon as well.")
]
await ctx.send(embed=embed)
I need my bot to remove the role "Free Agent" and add a new role.
to add a role to a user, you would use the .add_roles(role_name) and to remove a role from a user, you would use the .remove_roles(role_name).
an example:
#commmands.command()
async def role_remove(self, ctx, member:discord.Member):
await member.add_roles(role_name)
await member.remove_roles(role_name)
So i have seen another post with almost the exact same question however, mine is different.
I am fully aware there is a Patreon bot, however to my knowledge this is only valid on servers.
So lets say someone invites my bot, and tries a command that requires them to be a patron. How could my bot written in python do a check to see if they have become a patron for my product? And then set a role for them accordingly. Which i can then do the check on to allow them access to the command or not.
So essentially, it should do what the Patreon bot does, however would work on its own. Examples are such as the Dank Memer bot: which can be invited to any server and if one becomes a patron can use specific commands, otherwise you can't
I've looked around this topic for a while now and haven't been able to find any info on how to check if the user has become a patron or not.
Many thanks in advance!
If you know the patreons. just give them a role [patreons] or any name!
after giving them the role. copy the role id and paste it in patreons_role_id
ok, what this command does is, just it check for the [patreons] role in the member roles! if the [patreons] role is present! it will execute the #your code else it sends a custom message!
#client.event()
async def on_message(message):
if message.content == '!test':
is_patreon = False
for user_roles in message.author.roles:
if user_roles.id == patreons_role_id:
is_patreon = True
break
if is_patreon == True:#your code
else: await message.channel.send('THIS COMMAND IS ONLY AVAILABLE FOR PATREON!')
Let's assume you have a role for your patreons.
#client.command()
#commands.has_roles("PatreonRole")
async def commandname(ctx, args):
#do stuff
There is a way out, but its probably not the right one, so, If you know who your patreons are and you know their discord IDs as well, you can declare an if statement in the command similar to this -
#client.command()
async def your_command_name():
if member.id == #Your 1st Patreon's Discord ID:
#Your Code
elif member.id == #Your 2nd Patreon's Discord ID:
#Your code
else:
await ctx.send("You cannot use that command as you are not a patreon!")
But again this is probably not right way if you have a lot of patreons or you dont know their Discord IDs, but anyways this way was the only way I could come up with.
I hope that helped. :)
I've decided to try making my discord bot play music, but I've gotten stuck already. Mainly due to the fact I can't find any sources to help with the current version, I've been winging everything from the docs. However, I can't figure out how to check if the bot is connected to a voice channel.
I have tried if not Client.is_connected():, however that didn't work. If there are any updated sources to help me get the basics of discord.py's voice features, please give me a link :) Here is my code so far:
# ----- ATTEMPT AT VOICE COMMANDS ------
#discord.opus.load_opus() - what goes in bracket???
#client.command(name="join", pass_ctx=True)
async def join(ctx):
#if not is_connected(): - Client.is_connected() not working
user = ctx.message.author
vc = user.voice.channel
await vc.connect()
await ctx.send(f"Joined **{vc}**")
#else:
# await ctx.send("I'm already connected!")
#client.command(name="disconnect", pass_ctx=True)
async def disconnect(ctx):
# if not is_connected(): - once again can't work it out
vc = ctx.message.guild.voice_client # i don't even know how this worked :D
await vc.disconnect()
#else:
# await ctx.send("I'm not connected to any channels")
#client.command(name="play", pass_ctx=True)
async def play(ctx, songurl=None):
if not songurl: # this works at least
await ctx.send("Please specify a song")
return
if not is_connected(): # once again, how to check if bot is connected?
vc = ctx.message.author.voice.channel
if not vc: # i think this should work
await ctx.send("You're not in a voice channel!")
await vc.connect()
# haven't even worked out anything past this point and it's broken
ps: sorry for just dumping my whole vc section but i don't understand a lot
Really all that matters here is the play command, but I included the others just because (as you can see from my comments) I don't understand LOTS of what is going on. How should I go about this? Are there any good sources for the current version? Thanks in advance.
A bot can be connected to voice in multiple guilds at the same time, so you need to get the VoiceClient for the appropriate guild from Client.voice_clients and then check VoiceClient.is_connected:
def is_connected(ctx):
voice_client = get(ctx.bot.voice_clients, guild=ctx.guild)
return voice_client and voice_client.is_connected()
you could also do
client.command()
async def join(ctx):
user = ctx.message.author
vc = user.voice.channel
voice = discord.utils.get(client.voice_clients, guild=ctx.guild) # This allows for more functionality with voice channels
if voice == None: # None being the default value if the bot isnt in a channel (which is why the is_connected() is returning errors)
await vc.connect()
await ctx.send(f"Joined **{vc}**")
else:
await ctx.send("I'm already connected!")
After a bit of experimentation with my bot, I found that something similar to this may work for you. It's also pretty quick and easy.
if ctx.guild.voice_client in bot.voice_clients:
# Do something here
I used this with the on_message event, but it could likely work in your use case as well by doing:
if not ctx.guild.voice_client in bot.voice_clients:
# Do something else here