(discord.py)我想为一个不和谐的服务器做一个点名机器人
Posted
技术标签:
【中文标题】(discord.py)我想为一个不和谐的服务器做一个点名机器人【英文标题】:(discord.py) İ want to make a roll call bot for a discord server 【发布时间】:2021-01-25 19:22:02 【问题描述】:我想做一个不和谐的机器人。这就是我想要的:当我说“!yoklama 11.10.2020.txt”(或其他内容)时,机器人会向我发送我所在语音频道的参与者列表。[不是 1 个频道(我所在的频道)] 但我不知道我该怎么做。 然后我进行了一些研究,我发现我的要求有类似的事情。 但他们没有正常工作。 这是我找到的代码:
import discord
from discord.ext.commands import Bot
from discord.ext import commands
client = commands.Bot(command_prefix='!')
@client.command()
async def attendance(ctx, filename):
channel = client.get_channel(755129236397752441) # Replace with voice channel ID
with open(filename, 'w') as file:
for member in channel.members:
file.write(member.name + '\n')
client.run('mytoken')
此代码正在运行(当我运行时它们不会给我错误)但是当我说 !attendence test.txt bot 什么也没说,我的 python shell 对我说:
Ignoring exception in command attendance:
Traceback (most recent call last):
File "C:\Users\Yunus Emre KISA\AppData\Local\Programs\Python\Python38-32\lib\site-
packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(args, **kwargs)
File "C:\bot2\bot2.py", line 10, in attendance
for member in channel.members:
AttributeError: 'NoneType' object has no attribute 'members'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\Yunus Emre KISA\AppData\Local\Programs\Python\Python38-32\lib\site-
packages\discord\ext\commands\bot.py", line 903, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\Yunus Emre KISA\AppData\Local\Programs\Python\Python38-32\lib\site-
packages\discord\ext\commands\core.py", line 859, in invoke
await injected(ctx.args, **ctx.kwargs)
File "C:\Users\Yunus Emre KISA\AppData\Local\Programs\Python\Python38-32\lib\site-
packages\discord\ext\commands\core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError:
'NoneType' object has no attribute 'members
你们看,我什么都不懂,请帮帮我。 我发现了这些问题,但它对我不起作用: Discord Python bot creating a file of active member in a vocal channel
【问题讨论】:
【参考方案1】:@client.command()
async def test(ctx):
guild = ctx.guild
channel = discord.utils.get(guild.voice_channels, name='General')
for member in channel.members:
print('test')
我会做的而不是在命令中传递文件,只是在函数编写中:
with open("file.txt", "w") as f:
【讨论】:
感谢您的回答,但是当我对 bot 说 !test 时它没有给出任何反应(python shell 也是)这是我的代码:从 discord.ext.commands 导入不和谐从不和谐导入 Bot。 ext import commands client = commands.Bot(command_prefix='!') @client.command() async def test(ctx): guild = ctx.guild channel = discord.utils.get(guild.voice_channels, name='a' ) 对于 channel.members 中的成员:print('test') with open("file.txt", "w") as f:对于 channel.members 中的成员:file.write(member.name + '\n') client.run('mytoken')以上是关于(discord.py)我想为一个不和谐的服务器做一个点名机器人的主要内容,如果未能解决你的问题,请参考以下文章
如何使用新的 discord.py 版本获取不和谐服务器中所有成员的列表?
使用 discord.py 制作一个不和谐的机器人通过嵌入发送图像