@bot.command()
async def add(ctx, num1: int, num2: int):
result = num1 + num2
await ctx.send(f'The sum is {result}.')
@add.error
async def add_error(ctx, error):
if isinstance(error, commands.BadArgument):
await ctx.send('Please make sure to provide two numbers.')
elif isinstance(error, commands.MissingRequiredArgument):
await ctx.send('You forgot to provide one or both numbers.')
else:
await ctx.send('An unknown error occurred.')
import discord
from discord import Member
from discord.ext.commands import has_permissions, MissingPermissions
from discord.ext import commands
TOKEN = "Insert Your token here"
intents=discord.Intents.all()
intents.members=True
bot = commands.Bot(command_prefix="!",intents=intents)
#code to add role
@bot.command()
@commands.has_permissions(manage_roles = True)
async def addRole(ctx, user : discord.Member, role:discord.Role):
await user.add_roles(role)
await ctx.send(f" Added {role} to {user.ention}")
@addRole.error
async def role_error(self,ctx,error):
if isinstance(error, commands.MissingPermissions):
await ctx.send("You are not authorized for this action")
bot.run(TOKEN)
import asyncio
import discord
from discord.ext import commands, tasks
Token ="Insert Your token here"
prefix="!"
intents=discord.Intents.all()
bot = commands.Bot(command_prefix=prefix,intents=intents)
@bot.command(name='dm')
async def msg(ctx):
for guild in bot.guilds: # all servers of the bot
role = discord.utils.find(lambda r: r.name == 'Your role name', guild.roles)
for member in guild.members:
if role in member.roles:
await member.send("Dm to role member")
bot.run(Token)
Output:
使用 Discord 机器人禁止/踢出成员
要从服务器中删除成员,您需要将以下代码添加到 Python 文件中。
import discord
from discord import Member
from discord.ext import commands
from discord.ext.commands import has_permissions, MissingPermissions
TOKEN = "Insert Your token here"
intents=discord.Intents.all()
discord_client = commands.Bot(command_prefix='!',intents=intents)
@discord_client.command()
@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 from the server')
@kick.error
async def kick_error(ctx,error):
if isinstance(error,commands.MissingPermissions):
await ctx.send("You do not have required permission for the action performed")
discord_client.run(TOKEN)
首先,从不和谐库中导入成员和所需的权限方法,以便我们能够管理权限并解决用户问题。
We use @discord_client.command()使用机器人的客户端对象并通知它前面编写的代码是一个命令。 @has_permissions用于检查执行操作的用户是否有权这样做。 kick是执行从服务器中删除给定用户的操作的函数。
import discord
from discord import Member
from discord.ext import commands
from discord.ext.commands import has_permissions, MissingPermissions
TOKEN = "Insert Your token here"
intents=discord.Intents.all()
discord_client = commands.Bot(command_prefix='!',intents=intents)
@discord_client.command()
@has_permissions(ban_members=True)
async def ban(ctx, member:discord.Member,*,reason=None):
await member.ban(reason=reason)
await ctx.send(f'The User {member} has been banned from the server')
@ban.error
async def ban_error(ctx,error):
if isinstance(error,commands.MissingPermissions):
await ctx.send("You do not have required permission for the action performed")
discord_client.run(TOKEN)
Output:
在特定时间发送消息
要在特定时间发送消息,datetime除了库之外还需要discord到目前为止一直在使用的库。
import discord
from datetime import datetime
client = discord.Client()
token = "Insert Your token here"
channel_id=Insert Your channel id without quotes
def time_required():
while True:
current_time = datetime.now().strftime("%H:%M")
if current_time == "Insert required time":
print("time reached")
break
time_required()
@client.event
async def on_ready():
print("bot:user ready == {0.user}".format(client))
channel = client.get_channel(channel_id)
await channel.send("timed message")
client.run(token)
The time_required创建函数来确定何时发送消息。在达到所需时间之前,机器人将处于睡眠模式。 一旦到了发送消息的时间,on_ready 事件将被执行,消息“定时消息”将在代码中提到的通道 id 中发送。
from discord.ext import tasks
@tasks.loop(hours=1)
async def every_hour():
channel = bot.get_channel(123456) # Replace with your channel's ID
await channel.send("It's a new hour!")
every_hour.start()