I want to reset everyone's nickname in the server. How can I do? Which code should I use?
I'm new in coding. I didn't try anything.
#bot.command() # command decorator
#commands.has_permissions(change_nickname=True) # check for change nickname permission
async def reset(ctx):
for member in ctx.guild.members: # loop through every member in the guild
await member.edit(nick=None) # reset their nickname
Related
I want the bot to check if the command auther has the check or the permission
#commands.check(mefle7)
#commands.has_permissions(manage_messages=True)
Just use #commands.has_permissions(manage_messages=True)
Example:
#bot.command()
#commands.has_permissions(manage_messages=True)
async def clear(ctx, amount):
await ctx.channel.purge(limit=amount)
I wanted to make a command that edits the bot's old message trought another command, example:
user: !cmds
bot: *commands list*
user: !next
bot: *edits old message into a new one*
I have tried many times to make it myself, but I failed all the times, can anyone help me?
How I tried to make it:
#client.command
async def test():
embed=discord.Embed(title="Page1")
await ctx.send(embed=embed)
#client.event
async def on_command(message):
embed2=discord.Embed(title="Page2")
await client.process_commands(message)
if message.content == '$next':
await message.edit(content=embed2)
The on_command event is called whenever a command is called, you didn't register a next command, that's one, second of all on_command doesn't take message argument, it takes the Context(ctx).
An idea to make what you're asking for is having a cog with an instance variable that references to the previous message, then edit it when the next command is called.
class SomeCog(commands.Cog):
def __init__(self, client):
self.client = client
self._previous_message = None
#commands.command()
async def test(self, ctx):
message = await ctx.send("I'll edit this message when you type `$next`")
self._previous_message = message
#commands.command()
async def next(self, ctx):
if self._previous_message is None: # Exiting if the `test` command wasn't called before, i.e the message is a NoneType
return await ctx.send("You need to invoke first `$test`")
await self._previous_message.edit(content="Edited the message")
client.add_cog(SomeCog(client))
I'm guessing you could do it with global or bot vars but this is the best way to do it, in my opinion.
I want to make a mute command or any command that takes either a mention of a user id. both will work.
I really only need help with this part:
async def mute(ctx, member : discord.Member, *, reason=None):
so i can do /mute id or /mute #Member and both will work
all you have to do is :
#client.command(aliases=["m", "M", "Mute"]) #new one to karim
#commands.has_permissions(administrator=True)
async def mute(ctx, *, member: discord.Member):
command_name = "mute"
author = ctx.author
await member.edit(mute=True)
await ctx.send(f"{member.mention} is muted")
await ctx.message.add_reaction('✅')
and the unmute command :
#client.command(aliases=["unm", "UNM", "Unmute"]) #new one to karim
#commands.has_permissions(administrator=True)
async def unmute(ctx, *, member: discord.Member):
command_name = "unmute"
author = ctx.author
await member.edit(mute=False)
await ctx.send(f"{member.mention} is unmuted")
await ctx.message.add_reaction('✅')
I got it to work!
async def mute(ctx, member : discord.Member):
That piece of code was working I needed to turn intents on and turn on user tracking in the discord developer portal.
I'm making a giveaway bot and I cant get my bot to put what the user said into a field. For example, the bot says, "what do you want to giveaway?" then the user replies with something then the bot puts that "something" into its next reply.
take this simple bot
import discord
from bot import search_result, get_recent_query
class MyClient(discord.Client):
async def on_ready(self):
"""
showing message on console when the server start
:return:
"""
print('Logged on as {0}!'.format(self.user))
async def on_message(self, message):
# event listener, where sending message is a event
# we do not want the bot to reply to itself
if message.author == client.user:
return
print('Message from {0.author}: {0.content}'.format(message))
# checking the conditions, with which the user input starts
# if user input is hi, return hey
if message.content.startswith('hi'):
msg = 'hey {0.author.mention} '.format(message)
await message.channel.send(msg)
get the user reply from async def on_message(self, message) here message is what user reply and send the processed data to user as await message.channel.send(new_msg)
I don't know where to start like most things, but for example.
A user says "foo", the bot stores the message, and then says "bar". The closest I could get was this:
message = await channel.send('hmm…')
message_id = message.id
Though this stores the message sent by the bot, and not the user which triggered the function. It also doesn't store the message, just the ID. Please help.
Thanks in advance!
Edit: My goal is kinda like Sx4's suggest feature.
Hi :) not sure exactly what you want but seems like you are looking for the on message event? Here is an example. You should replace client with whatever you named your bot instance.
#client.event
async def on_message(message):
if message.author != client.user: # Avoids loops by checking that the message author is not the bot itself
# Code in here will run when a message is sent
await client.process_commands(message) # Runs all commands contained in the message
If you want to store every message, use something like this to set a variable that you can then do something with:
#client.event
async def on_message(message):
if message.author != client.user:
STORED_MESSAGE = message.content
# Do something with the variable here
await client.process_commands(message)
Or to respond to a message "foo" with "bar":
#client.event
async def on_message(message):
if message.author != client.user:
if message.content == "foo":
await message.channel.send("bar")
await client.process_commands(message)