首页 > 解决方案 > 无法使用 discord.py 服务器使某人静音

问题描述

基本上,每当我运行此代码时,它都会给我一个错误,我试图让代码服务器每隔 20 秒将我的一个朋友静音 5 秒,它表示定义用户的行有问题。

import discord
from discord.ext import commands
from discord.ext import commands, tasks
import random
import datetime
from datetime import date
import calendar
import time
import asyncio
from discord.ext.tasks import loop


client = commands.Bot(command_prefix='.')


@tasks.loop(seconds=20)
async def mute_person():
  user = await discord.Guild.fetch_member("670549896247509002", "670549896247509002")  # Get the Member Object
  await user.edit(mute=True)  # Mute
  await asyncio.sleep(20)  # Waits for 20 seconds then unmute.
  await user.edit(mute=False)  # Unmute

@client.event
async def on_ready():
  print("I am ready")
  mute_person.start()


client.run("Token")

标签: pythondiscorddiscord.py

解决方案


您没有使用正确的功能discord.Guild不存在,因为您正在使用它。主要用途必须是await guild.fetch_member(member_id)然后guild表示具有特定 ID 的公会对象。所以你需要先得到公会。


推荐阅读