从discord.py中的命令获取消息对象

时间:2019-03-14 11:56:17

标签: python discord.py

我正在搜索使用discord命令获取消息对象的方法。这是我的意思:

BotClient = discord.Client()
@BotClient.event
async def on_message(msg):
      print(type(msg)) # prints out <class 'discord.message.Message'>. As far as I know, that means that msg is an instance of this discord.message.Message class
      for I in msg.server.members: # I can call a members atribute
          print(I.nick) # prints out the nicknames of all present users
BotClient.run(token)

但这是带有命令的代码示例:

import discord
from discord.ext import commands
BotClient = commands.Bot(command_prefix = ".") 
@BotClient.command()
async def read(*args):
      print(type(args)) # That is a tuple, that contains only the content of the message
      for I in args:
          print(type(I)) # That is a string
BotClient.run(token)

我的问题是,我可以使用discord.py命令模块以某种方式获得相同的消息对象,而无需使用on_message()手动编写命令吗?

1 个答案:

答案 0 :(得分:0)

由于@PatrickHaugh,我终于弄清楚了如何做到这一点。这是与我的第一个代码上端完全相同的代码:

from discord.ext import commands
import discord
bot = commands.Bot(command_prefix='.')
@bot.event
async def on_ready():
      print("Ready!")
@bot.command(pass_context=True) # You need to allow to pass the Context object to the command function
async def test(cntx, *args): 
          for I in cntx.message.server.members: #Calling the list of members of current server, where the message was sent
              print(I.nick) # Printing out all nicknames
bot.run(token)
相关问题