我一直在尝试为服务器构建一个不一致的机器人,例如,你可以做“!发布文本+上传”,这将发送你在当前频道中从机器人输入的任何内容(加上上传的图像)。我在新的教程中用Python编写了一段曲折的代码,但到目前为止还没有固定下来。
我在下面得到的是一个尝试,让它从机器人上的命令发送一个图像作为开始,但是它甚至还没有运行到这一步。如果有人能帮我解决这个问题,得到想要的结果,那就太好了,如果这样更容易的话,我很乐意换个语言。到目前为止,我已经得到了重复命令的文本(参见:$hello,机器人将发送hello!)背。我将保留该命令以供进一步使用,但无法获得任何带有图像扩展名的内容)。我已经尝试了文件图像和url;尝试了不一致文档上的所有选项,但都没有成功。
import discord
import os
from replit import db
from discord.ext import commands
from discord.ext.commands import bot
import asyncio
import datetime as dt
client = discord.Client()
bot = commands.Bot(command_prefix='!')
@bot.event
async def on_ready():
    print('Connected!')
@client.event
async def on_ready():
  print('Hello world {0.user}'
  .format(client))
  
@client.event 
async def on_message(message):
  if message.author == client.user:
    return
  if message.content.startswith('$hello'):
    await message.channel.send('hello!')
@bot.command(pass_context=True)
async def test(ctx):
    await ctx.send('Working!', file=discord.File('image.png'))
client.run(os.environ['TOKEN'])我遵循的第一个教程让我使用replit来编写代码,存储数据,并在计算机不活动的情况下保持其在云基础上的运行。此时,只需使用$hello命令和任何类似命令,机器人就可以正常工作。与图像无关。
发布于 2021-06-22 15:04:12
首先,您应该只使用client或Bot之一,而不是两者都使用,bot是更好的选择,因为它有命令和事件。下面是使用commands.Bot(command_prefix='!')组织的代码
import discord
import os
from replit import db
from discord.ext import commands
import asyncio
import datetime as dt
bot = commands.Bot(command_prefix='!')
@bot.event
async def on_ready():
    print(f'Connected! {bot.user}')
  
@bot.event 
async def on_message(message):
    if message.author == bot.user:
        return
    #code here
# !hello
@bot.command()
async def hello(ctx):
    await ctx.send('Hey!')
# !test
@bot.command()
async def test(ctx):
    await ctx.send('Working!', file=discord.File('image.png'))
bot.run(os.environ['TOKEN'])您已经定义了两个实例,并且只运行了client。这就是!test不能工作的原因
https://stackoverflow.com/questions/68078114
复制相似问题