【问题标题】:how can i make my discord bot do the !say command?我怎样才能让我的不和谐机器人执行 !say 命令?
【发布时间】:2022-11-11 13:01:10
【问题描述】:

我以前从未编码过,所以我很新,我在 repli 上尝试 python,我搜索了很多,这就是我到目前为止所做的,但它不起作用。 (忽略反面部分)

import os        
import discord      
from keep_alive import keep_alive 

client = discord.Client(intents=discord.Intents.default())

@client.event  
async def on_ready():  
    print("I'm in")  
    print(client.user)   

@client.event  
async def on_message(message):  
    if message.content.startswith("!reverse"):  
        await message.channel.send(message.content[::-1])   

my_secret = os.environ['DISCORD_BOT_SECRET']  
client.run(my_secret)

keep_alive()  
my_secret = os.environ['DISCORD_BOT_SECRET']  
client.run(my_secret)   

async def on_message(message):  
    echo = message.content.split(" ", 1)[1]  
    if message.content.startswith("!say"):  
     await message.channel.send(echo)  

我希望机器人是这样的:
我:!说废话废话
机器人:废话废话

感谢任何回答的人

【问题讨论】:

    标签: python discord discord.py chatbot replit


    【解决方案1】:

    这里有很多问题

    • Intents.default() 不包含 message contents 意图,因此您将无法阅读消息。有关意图以及如何启用它们的更多信息,请阅读文档:https://discordpy.readthedocs.io/en/stable/intents.html
    • 您有两个 on_message 函数,它们不起作用。您不能有多个具有相同名称的函数。而是将它们合二为一。
    • 永远不要在client.run() 下放置任何代码——它永远不会被执行。
    • 你有两个client.run()。为什么?
    • 底部的on_message 缺少@client.event 装饰器,因此即使您没有其中的两个,它仍然不会被调用。
    • 为什么不使用Botcommands 而不是手动解析on_message 中的所有内容? https://discordpy.readthedocs.io/en/stable/ext/commands/index.html
    • Replit 不是用来运行机器人的,会给你带来很多麻烦。考虑将其托管在实际的 VPS 上(或在开发阶段 - 仅在本地)。

    【讨论】:

      【解决方案2】:

      好的,首先要进行一些其他更改。

      您没有使用命令,而是在寻找消息(技术上没有问题,但它可能会导致不必要的问题)

      我将修改代码,希望它能正常工作。

      import os        
      import discord      
      from keep_alive import keep_alive 
      
      bot= commands.Bot(command_prefix='!', intents=discord.Intents.default())
      
      @client.event  
      async def on_ready():  
          print("I'm in")  
          print(client.user)   
      
        
      @bot.command()
      async def reverse(ctx,*,message):
      
          await ctx.channel.send("message[::-1]")
      
      my_secret = os.environ['DISCORD_BOT_SECRET']  
      client.run(my_secret)
      
      keep_alive()  
      my_secret = os.environ['DISCORD_BOT_SECRET']  
      client.run(my_secret)  
       
      @bot.command()
      async def say(ctx, *, message):
        await ctx.message.delete()
        await ctx.channel.send(message)
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-04-15
        • 2021-07-09
        • 2021-07-24
        • 2018-04-18
        • 2011-08-09
        • 2020-11-17
        • 2021-04-06
        • 2021-11-15
        相关资源
        最近更新 更多