Discord 单词过滤机器人不会删除任何内容

发布于 2025-01-11 06:15:59 字数 761 浏览 0 评论 0原文

我为我们的一位成员制作了一个discord.py 机器人,作为一个笑话。我测试了一下,它根本不起作用。有人可以告诉我这段代码有什么问题吗?

from http import client
import os

import discord
from dotenv import load_dotenv

load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')

client = discord.Client()

@client.event
async def on_ready():
 print(f'{client.user} has connected to Discord!')

client.run(TOKEN)

@client.event
async def on_message(message):
 await client.process_commands(message) # add this if also using command decorators
 if message.author.id == 300677572683890688 and "tranime" in message.content.lower():
     await message.delete()
     await message.channel.send(f"You are in the process of behavioural therapy. please do not attempt to bypass it, {message.author.mention}")

     

I made a discord.py bot for one of our members, as a joke. im testing it out and it doesnt work at all. can someone tell me what can be wrong with this code?

from http import client
import os

import discord
from dotenv import load_dotenv

load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')

client = discord.Client()

@client.event
async def on_ready():
 print(f'{client.user} has connected to Discord!')

client.run(TOKEN)

@client.event
async def on_message(message):
 await client.process_commands(message) # add this if also using command decorators
 if message.author.id == 300677572683890688 and "tranime" in message.content.lower():
     await message.delete()
     await message.channel.send(f"You are in the process of behavioural therapy. please do not attempt to bypass it, {message.author.mention}")

     

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半枫 2025-01-18 06:15:59

构建机器人的结构在很多方面都是错误的,这可能会导致错误。

要运行机器人,

client.run(TOKEN)

始终位于代码的末尾

要处理命令,您可以将命令放在

await client.process_commands(message)

on_message 事件的末尾,尽管这里不需要这样做,因为您只需使用 discord.Client()

新的结构可能是:

client = discord.Client()

@client.event
async def on_ready():
 print(f'{client.user} has connected to Discord!')

@client.event
async def on_message(self, message):
    if message.author.id == 300677572683890688 and "tranime" in message.content.lower():
        await message.delete()
        await message.channel.send(f"Your message here")
    await client.process_commands(message)

client.run(TOKEN)

代码也应该正确缩进,否则可能会出现问题。

The structure, how you build the bot, is wrong in many ways, that could cause the error.

To run the bot,

client.run(TOKEN)

always belongs at the end of your code!

To process commands, you put

await client.process_commands(message)

at the end of your on_message event, even though this is not necessary here as you just use discord.Client().

A new structure could be:

client = discord.Client()

@client.event
async def on_ready():
 print(f'{client.user} has connected to Discord!')

@client.event
async def on_message(self, message):
    if message.author.id == 300677572683890688 and "tranime" in message.content.lower():
        await message.delete()
        await message.channel.send(f"Your message here")
    await client.process_commands(message)

client.run(TOKEN)

The code should also be indented correctly, otherwise there may be problems.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文