I have been trying to get this to work for about an hour now, but my dumb brain cannot figure it out. basically i want to get my bot to set it's own nickname when someone says (prefix)name (namechangeto)
#client.command(aliases=["nick", "name"])
async def nickname(ctx, name="Bot"):
user = guild.me()
await user.edit(nick=name)
any help appreciated
(also sorry for bad formatting, and i know this code probably makes some people cringe lol im sorry)
For me the problem in your code is that your variable guild is not defined, so you have to use ctx.guild to get the good guild. Besides that, discord.Guild.me is not a function (callable with ()) but an attribute, you just have to use guild.me.
So you can try to make :
user = ctx.guild.me
await user.edit(nick=name)
In addition to that, it's not really the problem but an advice, you can use async def nickname(ctx, *, name="Bot"): instead of async def nickname(ctx, name="Bot"): to allow user to set a nickname with spaces in it.
So your final code for me is that :
#client.command(aliases=["nick", "name"])
async def nickname(ctx, *, name="Bot"):
user = ctx.guild.me
await user.edit(nick=name)
Have a nice day!
Baptiste
Related
I'm coding a bot for a friend and they have asked me to make an 8ball command. You think it seems easy.. but they don't want the prefix included the command. So it would be like this:
BotName, do you think today will be a good day?
I've tried using #client.event but I don't know how to make it so that the user can say their own question, but have the bots name at the front of the question.
So like this:
BotName, do you think today will be a good day?
The BotName part will need to be included to trigger the event. Then they can say what they want. Like this (example was already given above):
BotName, do you think today will be a good day?
Here is some code I tried:
import discord
from discord.ext import commands
import random
class eightball(commands.Cog):
def __init__(self, client):
self.client = client
#commands.Cog.listener()
async def on_message(self,message):
#botname = [f"BotName, {message}?"] #tried this too
# if message.content in botname: #tried this too
if f'BotName, {message.author.content}?' in message.content:
responses = ['Responses here.']
await message.channel.send(f'{random.choice(responses)}')
await self.client.process_commands(message)
def setup(client):
client.add_cog(eightball(client))
If it's not possible then do not worry! (Sorry if I didn't explain as well as I could and if I sound dumb or something.)
I guess you can make it work with a bit more logic.
if message.content.startswith('BotName,'):
#rest of the code
Consider that if they # your bot, the string would be <#1235574931>, do you think today will be a good day?
So, it'll only work if they add specifically whatever BotName would be.
Also, cog listeners doesn't need await self.client.process_commands(message)
You might use events for your bot. Try to do this.
#command.Cog.listener()
async def on_message(self, message, question):
if message.content.startswith("8ball"):
answers=["yes", "no", "maybe"]
await message.channel.send(random.choice(answers)
Don’t forget to import random like this:
import random
I'm working on a bot that can give roles to users, but I'm not very well-versed in the discord.py syntax.
I've tried adapting code like this:
if message.content.startswith('!!role'):
member = message.author
var = discord.utils.get(message.guild.roles, name = "Role A")
member.add_role(var)
But this code doesn't fit my purpose, I'm trying to get a function more like this: roleGive(member,role) so then I could have a command in discord like !role #JohnSmith#0001 moderator, and it would give the user JohnSmith the 'moderator' role. Also, this function is going to be used elsewhere in the bot, so I'm trying to make the function reusable, so I could do something like (this is more-or-less pseudocode, I'm still learning):
user = "JohnSmith#0001"
if 20 > user.messagecount() >= 10:
roleGive(user,"10 Messages Gang")
if 30 > user.messagecount() >= 20:
roleGive(user,"20 Messages Gang")
I also do want to stress that I have looked at a lot of similar questions on this site, and either the code is outdated and no longer works, or it does not fit my purpose. Thanks for any help!
Try this for the function!
#import discord
async def roleGive(member: discord.Member, role: discord.Role):
await member.add_roles(role)
What I'm looking for is to make a help command that will give info for a specific role. Basically something like this:
/help -> General help info
/help mute -> Mute help info
/help ban -> Ban help info
I tried making different commands like this:
#commands.command()
async def help(self, ctx):
await ctx.channel.send('this is a help command')
and
#commands.command()
async def help_mute(self, ctx):
await ctx.channel.send('Mute help information')
but both commands would show the first command's message. -> this is a help command. Why is that and could it be fixed?
Any answer will be appreciated!
Yes, you can use Groups and Subcommands to do it like this, however I strongly advise against this because then you'd have to create a new help command every single time you create a command, and all of them are exactly the same (await ctx.send("help_for_this_command")).
One thing you can do is add the information in the help kwarg of a command when creating it:
#commands.command(help="Mute help information")
async def mute(ctx, ...):
This way, the default help will take care of this. However, you can't really change how the text looks, as it'll just be formatted by Discord's default help implementation, so you'll have to live with the codeblock it sends.
If you don't want that, then what I do recommend is creating your own custom Help command (not just a command named "help", but a class that inherits from the HelpCommand class & overrides functions where necessary). Then you can create a database (or JSON file) with the information for every command, and just get that.
{
"mute": "Mute help information",
"ban": "Ban help information",
...
}
and a very simplified version of your help command would revolve around something like this:
async def command_callback(self, ctx, *, command=None):
if command is not None:
if command in json_file:
await ctx.send(json_file[command])
else:
await ctx.send("This is not a known command.")
Which is a lot cleaner than a huge chain of if/else statements checking which command was called.
If you want to see how the default help is implemented (to get an example of how to create yours), you can take a look at the source on the Discord.py GitHub repo. Keep in mind you only have to override the functions you want to change - if the default behaviour does what you need then there's no need to copy paste the implementation into yours (as the base class's functions will just be called automatically).
EDIT:
Łukasz Kwieciński linked a useful guide with very easy steps explaining how to create your own HelpCommand. This will probably help you out a lot as well.
Instead, you could take it as a parameter to pass with the help command. This would work by including an optional input after the help command, it would look like /help ban or /help [category]
A simple way of doing this would be, if the optional parameter wasn’t passed, it would send just the help command with no category. When the user includes a provided category, it would send help with that category. Here is it implied to your command.
#commands.command()
async def help(self, ctx, category=None):
if category == None:
await ctx.channel.send('this is a help command')
return
if category == "ban":
await ctx.channel.send('this is a ban and this is how to use it....')
return
if category == "mute":
await ctx.channel.send('this is a mute command...')
return
else:
await ctx.channel.send('Please provide a valid category')
return
I'm trying to capture a users input, what they say in a message, and have it returned to them in a message from the bot. More specifically, when they run a command, it'll return what ever text they have entered after that.
So far, I'm here:
async def on_message(message):
if message.content.startswith["=ok"]:
await client.send_message(message.channel, message.content[6:])
...unfortunately, I believe this was valid for the previous version of Discord.py before the rewrite. Essentially I want someone to be able to run the command =pressf and have the bot return the message "Everyone, lets pay respects to (string)!" An event probably isn't the best way to go about this but I'm stumped.
I've been struggling to find a specific answer online for my issue so I greatly appreciate anyone who could point me in the proper direction. Thanks!
I would recommend using the newer Commands Extension, it is much simpler to implement what you are wanting. See this bit specifically for passing everything a user types after the command into a variable.
There is an official example I would recommend looking at here: https://github.com/Rapptz/discord.py/blob/master/examples/basic_bot.py
You should use commands instead of on_message event. Here is a simple command:
#client.command()
async def test(ctx):
await ctx.send('A Simple Command')
ctx parameter is the parameter that all commands must have. So, when you type =test, it will send to that channel A Simple Command.
If we come to what you try to do, you can use more parameters than ctx. Here is how you can do it:
#client.command()
async def pressf(ctx, *, mess):
await ctx.send(mess)
In this code, you have 1 more parameter called mess and also there's a *. That means mess parameter includes every message after =pressf. So when a user type =pressf Hello, it will send the channel Hello.
So i have a Discord Bot that is written in Python & Discord.py, now my question is, is it possible to do something like that:
Server_ID = 'TheServerID'
if ServerID = Server_ID:
leave
The most efficient way is to only check this if you're joining a guild right now, this is done with the discord.on_guild_join(guild) Reference. Then it's just checking the id and leaving if the id matches.
#client.event
async def on_guild_join(guild):
bad_id = 12345
if guild.id == bad_id:
await guild.leave()
In order to get the server id you have ctx.guild.id which is of type int, then you can compare it to any other int you want.
And if you want the bot to leave the serveur, you have to use guild.leave() which is a coroutine, so make sure to put await.
You code should looks like this:
server_id = <the id you want to compare>
if ctx.guild.id == server_id:
await ctx.guild.leave()
Make sure you put this code into an asynchronous function that would be maybe an event or a command.
If you have troubles to deal with discord.py, please refere to the documentation.