Toggling a server limited command - discord

#f spam
async def on_message(message):
if message.author.bot:
return
elif message.content.lower() == 'f':
await message.channel.send('f')
await bot.process_commands(message)
this is my current code for an f spam or sends an f each time a user does.
I was wanting to make this command toggleable and also server/guild limited if possible.
for example someone says !fspam and it gets toggled and switched off and when done the same again it gets turned on. OR it could be !fspam on/ !fspam off

You can use Command.enable, where you can use command.update to. This will raise the DisabledCommand error.
Also please dont just copy and paste the code, try understanding what I did.This will disable it for all the guilds to. If you want it per server then you will need to use a database.
For example:
#client.command()
async def enable(ctx,*,command):
command = client.get_command(command)
command.update(enabled=True)
await ctx.send(f"{command} enabled!")
#client.command()
async def disable(ctx,*,command):
command = client.get_command(command)
command.update(enabled=False)
await ctx.send(f"{command} disabled!")

Related

How do I implement a wait_for function into my command?

I am trying to make a bot that will pick a word from a text file and then scramble it and then the user unscrambles the word and types it. But I do not know how to implement the wait_for function into the command.
#client.command()
async def start(ctx):
await ctx.send('Time for chaos')
Unscrambled_word = random.choice(list(open('C:\\Users\\user\\Desktop\\Discord Bots\\Fake Speedjar\\words.txt')))
I suggest reading the documentation here
It comes with a good simple example.
There is another library called dpytools that has a helper (wait_for_author) for this kind of simple cases you can check it here
Basically the wait_for method will wait for something and then return it to you. In this case a "message".
def check(msg):
return msg.author == ctx.author and msg.channel == ctx.channel
message = await client.wait_for('message', check=check)
The check function takes only the awaited object(s) and returns a bool indicating if the message Is what you're expecting.
Please check the documentation for further information.

discord.py restart the command

I am making a discord bot and I tried to make a status command that will make my bot's status start changing, so I thought its working pretty well until I realized that I need it to restart and that I have no idea how to do that, so here is my code without the restart part:
#client.command()
async def status(ctx):
await client.change_presence(activity=discord.Streaming(name='firststatus', url='https://www.twitch.tv/my-channel-name'))
await asyncio.sleep(5)
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name='Secondstatus'))
so I did that but I don't know what to put at the end so it'll restart I guess it will be something like client.command.restart.
You can use a simple while loop or the built-in discord.py extension tasks
from discord.ext import tasks
#tasks.loop(seconds=5)
async def change_status():
await client.change_presence(activity=discord.Streaming(name='firststatus', url='https://www.twitch.tv/my-channel-name'))
await asyncio.sleep(5)
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name='Secondstatus'))
#client.command()
async def status(ctx):
change_status.start()
The change_status function will loop every 5 seconds and change the presence. You can stop it with change_status.stop()
Reference:
tasks.loop
Loop.start
Loop.stop

how to add reaction to a specifc user?

I've been working on this piece of code:
#client.event
async def lol(message,ctx):
if ctx.author.id == <user_id>:
await message.add_reaction('❤️')
else:
return
I am pretty sure that it is developed correctly, yet I am not getting the desired results.
I am pretty sure that if you check the console, an HTTP error would have been encountered. That's because you directly pasted the emoji in the function parameter. You have to add its unicode value. In your case:
await message.add_reaction(u'\u2764')
You are using an event decorator rather than a command, unless you were trying to detect if the user reacted, but its still completely off. What it looks like, the command that is called will send a message and the bot will react to it.
#client.command()
async def lol(ctx):
message = await ctx.send(f"{ctx.author.mention}, lol")
await message.add_reaction('😂')
If you then want to detect if the user had reacted to the emoji, you can use a event, but an on_reaction_add:
#client.event
async def on_reaction_add(reaction, user):
if reaction.emoji == '😂':
await user.send("Lol")

Discord.py mentioning mentioned user

I'm trying to make a "!welcome (mention member)" command, which will then send a predefined message, tagging the member I mentioned. I want it to look like this, if for example, I typed "!welcome #Elastic_Disaster" --> https://gyazo.com/8e055cd380f0dd1e608c77f590ce631c
This is the code I've tried, but it does nothing.
#bot.command()
#commands.has_permissions(administrator=True)
async def welcome (ctx, user = discord.Member):
member = str(ctx.member)
await ctx.send(f'{member} welcome to the server (and more welcome stuff here).')
If you want to specify a argument's class, you have to use : instead of =. Then, you can use discord.Member.mention to mention the member.
#bot.command()
#commands.has_permissions(administrator=True)
async def welcome (ctx, user: discord.Member):
await ctx.send(f'{user.mention} welcome to the server (and more welcome stuff here).')

Why can't I use my other commands after this rockpaperscissors one in discord.py?

I am writing a Discord bot but whenever I use this ,rockpaperscissors command, after the game has finished I can't use any of my other commands like ,help. Why is this?
elif message.content == ",rockpaperscissors":
await message.channel.send("```\nRock Paper Scissors\n~~~~~~~~~~~~~~~~~~~~~\nSend your move in the format ,[move] e.g. ,rock\n```")
#client.event
async def on_message(message):
_ = str(message.content).lower()[1::]
computer = random.choice(RPS)
await rockPaperScissorsChecker(message, _, computer)
return
It's not possible to tell from the code that you've given, but here are a couple possibilities of what might be causing it.
Scenario #1
You have multiple on_message events.
When using events, you don't need to write multiple ones - you can use if/elif statements to your advantage. If you do choose to have multiple, then the one that's been defined the latest in the script will be the "active" one.
Here's how to separate commands in on_message:
#client.event
async def on_message(message):
if message.author.bot:
return
elif message.content.lower().startswith(",rockpaperscissors"):
# code for the cmd here
elif message.content.lower().startswith(",cmd2"):
# second cmd code here
# etc. etc.
Scenario #2
You're using command decorators and on_message without processing the commands.
When overriding (writing your own) on_message event, you need to call Bot.process_commands() in order for the registered commands to work. Behind the scenes there's a default on_message event which has this already done, but because you're rewriting your own, you'll need to add it:
#client.event
async def on_message(message):
await client.process_commands(message)
# rest of your on_message code
References:
on_message()
Bot.process_commands() - "By default, this coroutine is called inside the on_message() event. If you choose to override the on_message() event, then you should invoke this coroutine as well."
commands.Command() - The command decorator.

Resources