【问题标题】:Discord Invoke command for discord bot不和谐机器人的不和谐调用命令
【发布时间】:2018-11-10 13:26:10
【问题描述】:

我创建了一个机器人,现在在我的 discord 服务器中,使用下面的代码。

我的问题是,一旦我与机器人进行了不和谐的聊天,我如何调用命令让机器人运行代码,为用户列表收集 csv?我不确定如何在聊天/服务器中调用机器人来获取列表。

"""A bot to list all members of a server."""
import csv
import time

from discord.ext import commands
from discord.ext.commands import Bot

# constants
PREFIX = "~"
TOKEN = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXx"

bot = Bot(command_prefix=PREFIX)


@bot.event
async def on_ready():
    print('Logged in as')
    print(bot.user.name)
    print(bot.user.id)
    print('------')


@bot.event
async def on_command_error(error, ctx):
    if isinstance(error, commands.CommandNotFound):
        return
    else:
        print(error)


@bot.command(pass_context=True)
async def stat(ctx):
    """Returns a CSV file of all users on the server."""
    await bot.request_offline_members(ctx.message.server)
    before = time.time()
    nicknames = [m.display_name for m in ctx.message.server.members]
    with open('temp.csv', mode='w', encoding='utf-8', newline='') as f:
        writer = csv.writer(f, dialect='excel')
        for v in nicknames:
            writer.writerow([v])
    after = time.time()
    await bot.send_file(ctx.message.author, 'temp.csv', filename='stats.csv',
                        content="Here you go! Check your PM's. Generated in {:.4}ms.".format((after - before)*1000))


if __name__ == '__main__':
    bot.run(TOKEN)

【问题讨论】:

    标签: discord discord.py


    【解决方案1】:

    你的意思是运行你的stat 命令吗?由于您的前缀是~,因此您可以通过在频道中输入~stat 来调用该命令。

    【讨论】:

      猜你喜欢
      • 2018-07-21
      • 2017-05-31
      • 2021-10-21
      • 2022-01-15
      • 2021-06-17
      • 2018-03-20
      • 2021-10-05
      • 2019-09-04
      • 2020-11-18
      相关资源
      最近更新 更多