大家好。我制作了一个机器人,当连接到语音通道时,“我们要玩什么?”识别用户启动的游戏,将参与者移动到游戏名称的频道。但无论我多么努力,member.activity 或 member.activities 总是空的(有几行来自神经网络和互联网用户类似问题的答案)。由于这是我第一次制作机器人,我无法真正理解discord.py文档或其他人的代码。我附上脚本:
import discord
from discord.ext import commands
import asyncio
intents = discord.Intents.default().all()
intents.voice_states = True
intents.members = True
intents.presences = True # Убедитесь, что этот интент включен
bot = commands.Bot(command_prefix='--', intents=intents)
@bot.event
async def on_ready():
print(f'Logged in as {bot.user}')
async def check_activity(member):
while True:
if member.voice and member.voice.channel.name == "Во что будем играть?":
print(f"Checking activity for {member.name} in 'Во что будем играть?'")
game_name = None
for m in member.voice.channel.members:
print(f"Checking member: {m.name}, activities: {m.activities}") # Выводим активность участника
for activity in m.activities:
if isinstance(activity, discord.Game):
game_name = activity.name
print(f"Found game '{game_name}' for {m.name}")
break
if game_name:
existing_channel = discord.utils.get(member.guild.voice_channels, name=game_name)
if not existing_channel:
print(f"Creating new voice channel '{game_name}'")
existing_channel = await member.guild.create_voice_channel(name=game_name)
if member.voice.channel != existing_channel:
print(f"Moving {member.name} to channel '{existing_channel.name}'")
await member.move_to(existing_channel)
else:
print(f"{member.name} is already in the channel '{existing_channel.name}'")
else:
print(f"{member.name} is not in the channel 'Во что будем играть?'")
await asyncio.sleep(1)
@bot.event
async def on_voice_state_update(member, before, after):
if after.channel and after.channel.name == "Во что будем играть?":
print(f"{member.name} has joined the voice channel 'Во что будем играть?'")
bot.loop.create_task(check_activity(member))
# Замените 'YOUR_TOKEN' на токен вашего бота (я просто не стал вставлять его, он есть в запускаемом файле)
bot.run('')