从 Discord.py 中的 Discord 机器人状态中删除“正在播放”前缀

问题描述 投票:0回答:1

我正在使用discord.py开发Discord机器人,并且我正在尝试设置机器人的状态,而不使用Discord添加的默认“正在播放”、“正在观看”、“正在收听”等前缀。我的目标是提供一个简单的状态消息,例如“准备好提供帮助!| B!”,但我在实现此目标时遇到了问题。

目前的做法:

当我使用discord.Game设置状态时,它会添加“正在播放”前缀:

await client.change_presence(activity=discord.Game(name=current_status))

这会导致类似以下的状态:“Playing Ready to help! | B!”,这不是我想要的。

尝试的解决方案:

我尝试将discord.Activity与ActivityType.custom一起使用,但导致没有显示状态:

await client.change_presence(activity=discord.Activity(type=discord.ActivityType.custom, name=current_status))

最终目标可视化:

我的目标是类似于这个机器人的状态显示,它不显示 任何前缀:Pancake bot no prefix
status

但是,我目前的方法会导致以下结果:My
bot

MRE:

import discord
from discord.ext import commands
import asyncio
import random

async def change_presence():
    statuses = ["Ready to help! | B!", "Here to help! | B!", "Your friendly bot! | B!", "Ready for commands! |B!"]
    while True:
        current_status = random.choice(statuses)
        await client.change_presence(activity=discord.Game(name=current_status))  # This adds the 'Playing' prefix
        # await client.change_presence(activity=discord.Activity(type=discord.ActivityType.custom, name=current_status))  # No status shown
        await asyncio.sleep(7200)

intents = discord.Intents.all()
client = commands.Bot(command_prefix=("B!", "b!", "bot!", "Bot!"), intents=intents)

@client.event
async def on_ready():
    print("Bot is starting...")
    client.loop.create_task(change_presence())
    print("Bot is ready for action!")

client.run("your_token_here")

在discord.py(甚至在JavaScript中)有没有办法实现没有前缀的状态?任何帮助或指示将不胜感激。谢谢!

python discord discord.js discord.py
1个回答
0
投票

您可以使用

discord.CustomActivity
类和
ActivityType.custom
设置自定义状态,例如

activity = discord.CustomActivity(name="pancake.gg | p!help")
await client.change_presence(activity=activity)

另一种方法是使用默认的

discord.Activity
类,例如

activity = discord.Activity(
   type = discord.ActivityType.custom,
   name = "Custom Status",  # does nothing but is required
   state = "pancake.gg | p!help"
)
await client.change_presence(activity=activity)

但是第一种方法是推荐的。 所以你修改后的代码将是

import discord
from discord.ext import commands
import asyncio
import random

async def change_presence():
    statuses = ["Ready to help! | B!", "Here to help! | B!", "Your friendly bot! | B!", "Ready for commands! |B!"]
    while True:
        current_status = random.choice(statuses)
        await client.change_presence(activity=discord.CustomActivity(name=current_status))  # This doesn't add any suffixes
        # await client.change_presence(activity=discord.Activity(type=discord.ActivityType.custom, name=current_status))  # No status shown
        await asyncio.sleep(7200)

intents = discord.Intents.all()
client = commands.Bot(command_prefix=("B!", "b!", "bot!", "Bot!"), intents=intents)

@client.event
async def on_ready():
    print("Bot is starting...")
    client.loop.create_task(change_presence())
    print("Bot is ready for action!")

client.run("your_token_here")
© www.soinside.com 2019 - 2024. All rights reserved.