【问题标题】:Discord bot that returns guild owner's username discord.py返回公会所有者用户名 discord.py 的 Discord 机器人
【发布时间】:2021-03-09 22:15:54
【问题描述】:

大家好,我正在尝试编写一个代码,让我获得不和谐的服务器所有者,但它给了我“无”

import discord

client = discord.Client()
TOKEN = 'token'

@client.event
async def on_message(message):
    if message.content.find("getowner") != -1:
        await message.channel.send(str(message.guild.owner))


client.run(TOKEN)

谁能帮我解决这个机器人谢谢!! 我想通过在文本频道中输入 getowner 来获取不和谐服务器的所有者。

【问题讨论】:

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


【解决方案1】:

我建议将其用作命令而不是 on_message,您可以这样做:

from discord.ext import commands

token = "Token"
client = commands.Bot(command_prefix="!") #Use any prefix

@client.command(pass_context=True)
async def getOwner(ctx):
    #await ctx.channel.send(str(ctx.guild.owner.display_name))
    await ctx.channel.send(str(ctx.guild.owner))

client.run(TOKEN)

但如果你不想使用命令,你可以使用正则表达式并将其保留为:

import discord
import re

client = discord.Client()
TOKEN = 'token'

@client.event
async def on_message(ctx):
    if re.match("getowner", ctx.content):
        await ctx.channel.send(str(ctx.guild.owner))

client.run(TOKEN)

编辑:还可以尝试将intents 与您的机器人代码一起使用。

import discord
import re

intents = discord.Intents.default()
intents.members = True

client = discord.Client(intents=intents)
TOKEN = "token"

@client.event
async def on_message(ctx):
    if re.match("getowner", ctx.content):
        await ctx.channel.send(str(ctx.guild.owner))

client.run(TOKEN)

可以使用命令版本进行相同的更改。

【讨论】:

  • 是的,感谢您的帮助,但它返回我没有!而不是服务器所有者的用户名
  • 尝试在您的代码中使用intents,就像在编辑中一样
【解决方案2】:

很简单;

from discord.ext import commands

@client.command(name="getowner")
async def getowner(ctx):
   await ctx.send(str(ctx.guild.owner))
         

【讨论】:

    猜你喜欢
    • 2021-09-05
    • 2021-01-29
    • 2019-06-03
    • 2021-04-30
    • 1970-01-01
    • 2021-02-27
    • 2021-03-29
    • 2021-07-18
    • 2021-05-24
    相关资源
    最近更新 更多