role specific command - python

role specific command yes its working finally got it.
from discord.ext import commands
bot = commands.Bot('?')
#bot.command(pass_context=True)
#commands.has_any_role("Admin", "Moderator")
async def hello(ctx):
await bot.say("Hello {}".format(ctx.message.author.mention))

You can use the discord.ext.commands extension, which offers a has_any_role decorator.
from discord.ext import commands
bot = commands.Bot('?')
#bot.command(pass_context=True)
#commands.has_any_role("Admin", "Moderator")
async def hello(ctx):
await bot.say("Hello {}".format(ctx.message.author.mention))

Related

Your message was not delivered. Discord bot

I'm making a discord bot. when I send commands on the server from my account with the owner role, then everything is OK, it is executed.
photo_1
If I switch to another account that has the everyone role, then the bot stupidly does not react.
photo_2
here is a piece of code:
import discord
from discord.ext import commands
from dislash import InteractionClient, Option, OptionType
bot = commands.Bot(command_prefix='!')
inter_client = InteractionClient(bot)
#bot.event
async def on_ready():
print('Bot is launch!')
#inter_client.slash_command(description="Says Hello")
async def hello(ctx):
await ctx.respond(content='asfd', ephemeral=True)
bot.run(token)

Making a Slash Command appear in Discord UI

On discord, the module discord.ext.commands lets you create commands. I tried to create a command, with prefix="/", although it won't appear in discord's UI.
Code:
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix="/")
#bot.command(name="kick", description="Kick a user to your wish.")
#commands.has_permissions(kick_members=True)
async def kick(ctx, member: discord.Member, *, reason=None):
await member.kick(reason=reason)
await ctx.send(f"The user #{member} has been kicked. Reason: {reason}.")
bot.run("mytoken")
It won't pop up.
But I want to do it like this:
You need to install discord-py-slash-command and then put the code to import it from discord-py-slash-command import SlashCommands. You can refer the code below:
import discord
from discord_slash import SlashCommand
client = discord.Client(intents=discord.Intents.all())
slash = SlashCommand(client, sync_commands=True) # Declares slash commands through the client.
guild_ids = [1234567890] # Put your server IDs in this array.
#slash.slash(name="ping", guild_ids=guild_ids)
async def _ping(ctx):
await ctx.send("Pong!")
client.run("token")

How to use on_message together with client.command?

#client.event
async def on_message(message):
await client.process_commands(message)
I have no idea how to use this.
The above code is totally fine. If you haven't DEFINED Your bot yet, You have to do it.
Here is the simple code you can modify according to your needs.
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix = 'YOUR PREFIX')
#bot.command()
async def ling(ctx):
await ctx.send("Mabel")

Discord.py bot doesn't recognize commands from cogs

I've been trying to figure out how cogs work with the discord.py rewrite and I managed to get a bot working with only one cog. The problem is, the bot recognizes commands from one cog, but not the other.
importing the cogs in bot.py
cogs = [
'cogs.basic',
'cogs.mod']
#bot.event
async def on_ready():
print("Arthur Morgan")
print("version: "+version)
game = discord.Game("rebuilding.....")
await bot.change_presence(status=discord.Status.idle, activity=game)
for extension in cogs:
bot.load_extension(extension)
return
inside basic.py commands inside here seem to work
import discord
from discord.ext import commands
class Basic(commands.Cog):
def __init__(self,bot):
self.bot = bot
#commands.command(name='ping', aliases=['p'])
async def ping(self, ctx):
await ctx.send("Pong! :ping_pong:")
def setup(bot):
bot.add_cog(Basic(bot))
commands in mod.py output this
import discord
from discord.ext import commands
class Mod(commands.Cog):
def __init__(self,bot):
self.bot = bot
#commands.command()
async def pong(self, ctx):
await ctx.send("Ping!")
def setup(bot):
bot.add_cog(Mod(bot))
does anybody know how to fix this?
for extension in cogs:
bot.load_extension(extension)
return
The coroutine stops when it reaches return, after loading only one cog.

Discord.py Self Bot using rewrite

I'm trying to make a selfbot using discord.py rewrite.
I'm encountering issues when attempting to create a simple command.
I'd like my selfbot to respond with "oof" when ">>>test" is sent.
Here is my code:
import asyncio
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix=(">>>"), self_bot=True)
#bot.event
async def on_ready():
print("Bot presence t u r n e d on ( ͡° ͜ʖ ͡°)")
#bot.command()
async def test(self, ctx):
await self.bot.say("oof")
bot.run("my token", bot=False)
A self-bot isn't a bot that uses self, it's a bot that logs in using your credentials instead of a bot account. Self bots are against the Discord TOS (and you're not doing anything that requires one), so you should set up a bot account through their website and use a bot account for your bot.
That said, bot.say has been replaced by ctx.send in rewrite, and you're not in a cog so you shouldn't use self as all.
from discord.ext import commands
bot = commands.Bot(">>>", self_bot=True)
#bot.event
async def on_ready():
print("Bot presence t u r n e d on ( ͡° ͜ʖ ͡°)")
#bot.command()
async def test(ctx):
await ctx.send("oof")
bot.run("my token", bot=False)

Categories

Resources