Making say command bot owner only - discord.py

I am trying to make my !say command only work for the bot owner. This is what I currently have
#bot.command(pass_context = True)
async def say(ctx, *args):
mesg = ' '.join(args)
await bot.delete_message(ctx.message)
return await bot.say(mesg)
The code works but I want to make it so only I (the bot owner) can run the command.

You can also use the decorator #is_owner().
#bot.command(pass_context = True)
#commands.is_owner()
async def say(ctx):
your code...

Try doing it this way by adding an if statement
#bot.command(pass_context=True)
async def say(ctx):
if ctx.message.author.id =='bot owner id':
then execute the following code

This is how I did it - do comment if this does not work as intended.
#client.command()
#commands.is_owner()
async def say(ctx, *, message):
await ctx.send(f"{message}")
Have fun with your coding! Sorry for the late reply, I was reading old questions and seeked for unsolved issues
(remade)

The documentation for check in the 1.0 docs has the below example (slightly modified.)
def user_is_me(ctx):
return ctx.message.author.id == "Your ID"
#bot.command(pass_context = True)
#commands.check(user_is_me)
async def say(ctx, *args):
mesg = ' '.join(args)
await bot.delete_message(ctx.message)
return await bot.say(mesg)
How to find your ID

Related

trying to make an afk command for my bot but i am getting this error

async def afk(self, ctx, *args):
msg = ' '.join(args)
self.data.append(ctx.author.id)
self.data.append(msg)
await ctx.author.edit(nick=f'[AFK] {ctx.author.name}')
await ctx.send("afk set!")
#commands.Cog.listener()
async def on_message(self, message):
for i in range(len(self.data)):
if (f"<#{self.data[i]}>" in message.content) and (not message.author.bot):
await message.channel.send(f"<#{self.data[i]}> is away right now, they said: {self.data[i+1]}")
return None
break
#commands.Cog.listener()
async def on_typing(self, channel, user, when):
if user.id in self.data:
i = self.data.index(user.id)
self.data.remove(self.data[i+1])
self.data.remove(user.id)
nick = ctx.author.name.replace('[AFK]', '')
await ctx.author.edit(nick=nick)
await channel.send(f"{user.mention}, Welcome back!")
But it is showing ctx is not found nick = ctx.author.name.replace('[AFK]', '') i have been trying various methods to solve it, but i am not able to fix this, pls help me
You're using ctx in a func which doesnt have a ctx arg, you should instead use
nick = user.author.name.replace('[AFK]', '')
await user.author.edit(nick=nick)

Get a message sent to only one guild when my bot is added to a server

I am new to coding so sorry if this is easy to do. But what I am wanting to have my bot do is send a message to my private guild when it is added or removed from another guild. Also if it could say the name of said guild that would help as well.
#client.event
async def on_guild_join(guild):
chn = client.get_channel(YOUR_PRIVATE_GUILD_CHANNEL_ID)
embed = discord.Embed(title=f"Bot Invited",description=f"""
**User Name:** {bot_entry[0].user.name}
**User ID:** {bot_entry[0].user.id}
**Server Name:** {guild.name}
**Server ID:** {guild.id}
**Members Count:** {len(guild.members)}
""", colour=0x2dc6f9)
embed.set_footer(icon_url=guild.icon_url, text=guild.name)
embed.set_author(name=f"{bot_entry[0].user.name}#{bot_entry[0].user.discriminator}", icon_url=bot_entry[0].user.avatar_url)
embed.timestamp = datetime.datetime.utcnow()
server_invite = await guild.text_channels[0].create_invite(max_age = 0, max_uses = 0)
await chn.send(f"{server_invite}", embed=embed)
Something like this?
#client.event
async def on_guild_join(guild):
await client.get_channel(idchannel).send(f"{client.name} has joined")

How do I get bot to read mention?

If someone mentions me how do I get the bot to read that? like if someone mentions me I want the bot to say something back. How would I do that?
#client.event
me = '<#user_id>'
async def on_message(message):
member = message.author.id
if message.content == me:
await message.channel.send('my master will be back shortly')
else:
return
await client.process_commands(message)
You can use discord.Message.mentions. It returns a list of discord.Member that were mentioned in message.
#client.event
async def on_message(message):
me = message.guild.get_member(<user id>)
if me in message.mentions:
await message.channel.send('my master will be back shortly')
await client.process_commands(message)
Reference
discord.Message.mentions

auto mute a member if they say a specific word discord py rewrite

import discord
import re
from itertools import cycle
class Status(commands.Cog):
def __init__(self, client):
self.client = client
#commands.Cog.listener()
async def on_message(self, ctx):
if ctx.author.id == self.client.user.id:
return
if re.search("\.\.\.", ctx.content):
await ctx.send("you're... gonna get... muted... if you.. talk... like.. this...")
user = ctx.message.author
# print(str(user))
# print(str(message.content))
muted_role = discord.utils.get(ctx.author.guild.roles, name="Muted")
await self.client.add_roles(ctx.author, muted_role)
What I want is to temporarily mute a user if they use ellipses in a message they send. ctx.send doesn't work, the bot does not send a message to the channel. It says that self.client.add_roles doesn't exist.
Muted is a role that I have created that does not have any send message permissions.
Any idea why? Some help would be hugely appreciated. I'm using
AttributeError: 'Message' object has no attribute 'send' this is the error I get
[EDIT]
#commands.Cog.listener()
# #commands.command()
async def on_message(self, message):
if message.author.id == self.client.user.id:
return
if re.search("\.\.\.", message.content):
await message.channel.send("you're... gonna get... muted... if you.. talk... like.. this...")
user = message.author
muted_role = discord.utils.get(message.guild.roles, name="Muted")
await user.add_roles(muted_role, reason="you know what you did", atomic=True)
I took a look at the documentation and did this, it works, thank you for the support :)
There were many mistakes, please have a look at the documentation.
I have correct them for you -
#commands.Cog.listener()
async def on_message(self, message):
if message.author.id == self.client.user.id:
return
else:
await message.channel.send("you're... gonna get... muted... if you.. talk... like.. this...")
user = message.author
print(str(user))
print(str(message.content))
muted_role = discord.utils.get(message.guild.roles, name="Muted")
await self.user.add_roles(muted_role)
Let me know if you do still get any errors.
Last edit-
I tried this command for myself outside a cog, and it works perfectly fine :)
#client.event
async def on_message(message):
if message.author.id == client.user.id:
return
elif "test" in message.content:
await message.channel.send("you're... gonna get... muted... if you.. talk... like.. this...")
user = message.author
print(str(user))
print(str(message.content))
muted_role = discord.utils.get(message.guild.roles, name="Muted")
await user.add_roles(muted_role)
else:
return
await client.process_commands(message)

I can't make the bot write that you don't have rights to use the command

I created a lot of moderation commands, and only allowed admins to use them.I also really want to make such a scheme:If a person who does not have the right to use the command writes it,the bot sends a message that you do not have the right to use this command.Please help me figure this out!
#client.command()
#commands.has_permissions(view_audit_log=True)
async def ban(ctx,member:discord.Member,reason):
emb = discord.Embed(title="ban",color=0xff0000)
emb.add_field(name='Модератор',value=ctx.message.author.mention,inline=False)
emb.add_field(name='Нарушитель',value=member.mention,inline=False)
emb.add_field(name='Причина',value=reason,inline=False)
await member.ban()
await channel.send(embed = emb)
You can use a error handler.
#client.command()
#commands.has_permissions(view_audit_log=True)
async def ban(ctx,member:discord.Member,reason):
emb = discord.Embed(title="ban",color=0xff0000)
emb.add_field(name='Модератор',value=ctx.message.author.mention,inline=False)
emb.add_field(name='Нарушитель',value=member.mention,inline=False)
emb.add_field(name='Причина',value=reason,inline=False)
await member.ban()
await channel.send(embed = emb)
#ban.error
async def ban_error(ctx, error):
if isinstance(error, commands.MissingPermissions):
await ctx.send('You do not have the required permissions to use this command.')

Resources