如何使自定义状态discord.py

时间:2020-02-04 10:07:44

标签: python python-3.x discord discord.py

为什么我的代码不起作用?

@bot.event
async def on_ready():
    print('Bot is now working!')
    await bot.change_presence(activity=discord.CustomActivity(name='Custom status' ,emoji='?️'))

给我一​​个错误。

Ignoring exception in on_ready
Traceback (most recent call last):
  File "/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/discord/client.py", line 270, in _run_event
    await coro(*args, **kwargs)
  File "main.py", line 30, in on_ready
    await bot.change_presence(activity=discord.CustomActivity(name='Custom status' ,emoji='?️'))
AttributeError: module 'discord' has no attribute 'CustomActivity'

错误如何解决?

5 个答案:

答案 0 :(得分:3)

我迟到了, WayToDoor是正确的,机器人不能使用自定义状态,我发现他们“可以使用它”,但它是不可见的,除了机器人可以看到它是自定义状态之外,您还将在机器人配置文件上看到“自定义状态”。

机器人可以进行播放,观看,收听和流媒体播放

您现在还可以选择使用“竞争方式”(类型为5)。我在文档中还看不到它,因此我假设它尚未实现。

#this is how "Competing in" is set.
discord.Activity(name="Test", type=5)

这应该有效。

答案 1 :(得分:2)

要制作您应该使用的自定义活动:

activityvar = discord.Activity(type=discord.ActivityType.custom,state="NAMEOFMYACTIVITY")
await bot.change_presence(activity=activityvar)

出于某种原因,自定义活动使用状态参数而不是名称来定义名称

答案 2 :(得分:1)

机器人尚不能使用自定义状态。请改用addOnSuccessListeneraddOnCompleteListener。参见https://github.com/Rapptz/discord.py/issues/2400

答案 3 :(得分:1)

试试这些:

# Setting `Playing ` status
await bot.change_presence(activity=discord.Game(name="a game"))

# Setting `Streaming ` status
await bot.change_presence(activity=discord.Streaming(name="My Stream", url=my_twitch_url))

# Setting `Listening ` status
await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name="a song"))

# Setting `Watching ` status
await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name="a movie"))

答案 4 :(得分:-3)

试试这个:

@bot.command(pass_context=True)
async def Self_Clock(ctx):
    print("[+]: Status Changer Loaded")
    await ctx.message.delete()
    from datetime import datetime
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7',
        'Content-Type': 'application/json',
        'Authorization': token,
    }
    request = requests.Session()
    while True:
        now = datetime.now()
        current_time = now.strftime("%H:%M:%S")
        setting = {
            'status': "online",
            "custom_status": {"text": f"[+]: My current time: {current_time}"}
        }
        request.patch("https://canary.discordapp.com/api/v6/users/@me/settings",headers=headers, json=setting, timeout=10)
        time.sleep(1)