首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >不一致机器人从内置命令发送带有图像的文本消息(Python;可以用另一种语言重做)

不一致机器人从内置命令发送带有图像的文本消息(Python;可以用另一种语言重做)
EN

Stack Overflow用户
提问于 2021-06-22 13:41:56
回答 1查看 175关注 0票数 0

我一直在尝试为服务器构建一个不一致的机器人,例如,你可以做“!发布文本+上传”,这将发送你在当前频道中从机器人输入的任何内容(加上上传的图像)。我在新的教程中用Python编写了一段曲折的代码,但到目前为止还没有固定下来。

我在下面得到的是一个尝试,让它从机器人上的命令发送一个图像作为开始,但是它甚至还没有运行到这一步。如果有人能帮我解决这个问题,得到想要的结果,那就太好了,如果这样更容易的话,我很乐意换个语言。到目前为止,我已经得到了重复命令的文本(参见:$hello,机器人将发送hello!)背。我将保留该命令以供进一步使用,但无法获得任何带有图像扩展名的内容)。我已经尝试了文件图像和url;尝试了不一致文档上的所有选项,但都没有成功。

代码语言:javascript
运行
复制
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命令和任何类似命令,机器人就可以正常工作。与图像无关。

EN

回答 1

Stack Overflow用户

发布于 2021-06-22 15:04:12

首先,您应该只使用clientBot之一,而不是两者都使用,bot是更好的选择,因为它有命令和事件。下面是使用commands.Bot(command_prefix='!')组织的代码

代码语言:javascript
运行
复制
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不能工作的原因

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68078114

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档