如何通过python删除不和谐的聊天次数



我想创建一个机器人,它可以删除最近聊天和历史聊天的次数

import discord  
import random
class MyClient(discord.Client):
async def on_ready(self):
print('Logged on as {0}!'.format(self.user))
async def on_message(self, message):  
channel = message.channel.name
restricted_channels = ["command-bot"]  
prefix = "-"  # Replace with your prefix
# If the message starts with the prefix
if message.content.startswith(prefix):
if channel in restricted_channels:  
command = message.content[len(prefix):]  
if command.startswith("clear"):
await message.delete()

我试过这个

if command.startswith("clear"):
await message.delete()

但它只删除具有命令"的聊天;清除";

首先,我个人会更改代码的结构/布局。这是为了更容易阅读,更容易更改和添加命令/不同的功能。这是我设置机器人的方式,也是我看到许多其他机器人设置的方式:

import discord
client = commands.Bot(command_prefix='your prefix', intents=discord.Intents.all()) # creates client

@client.event # used commonly for different events such as on_ready, on_command_error, etc...
async def on_ready():
print('your bot is online') # basic on_ready event and print statement so you can see when your bot has gone online

现在我们已经完成了这一部分,让我们开始执行您试图执行的清除/清除命令。我的机器人中有一个,通常看起来像这样:

@client.command() # from our client declaration earlier on at the start
@commands.has_permissions(moderate_members=True) # for if you want only certain server members to be able to clear messages. You can delete this if you'd like
async def purge(ctx, amount=2): # ctx is context. This is declared so that it will delete messages in the channel which it is used in. The default amount if none is specified in the command is set to 2 so that it will delete the command call message and the message before it
await ctx.channel.purge(limit=amount) # ctx.channel.purge is used to clear/ delete the amount of messages the user requests to be cleared/ deleted within a specific channel

希望这能有所帮助!如果你有任何问题,请告诉我,我会尽我所能帮助

相关内容

最新更新