python中的函数,返回不和用户id上的不和用户标签作为输入



这是代码:-

from discord.ext import commands
bot=commands.Bot()
user = bot.get_user(431677941722906625) 
discrim = user.discriminator

这是错误:-

Traceback (most recent call last):
File "c:/Users/hacks/PycharmProjects/just testing new stuff/id.py", line 2, in <module>
bot=commands.Bot()
TypeError: __init__() missing 1 required positional argument: 'command_prefix'

我不明白代码出了什么问题

回复您的评论

command_prefix是命令的前缀。要使您的代码能够启用意图,请使用令牌运行bot并在命令或on_ready事件中获取用户,如果您只是想从它的ID中获取用户,最好的选择是使用discord API。

启用意图和设置前缀

import discord
from discord.ext import commands
intents = discord.Intents.default()
intents.members = True 
bot = commands.Bot(
command_prefix="!", # Change the prefix accordingly
intents = intents   # The intents we defined above
)

还记得在开发人员门户中启用特权成员意图,下面是如何

现在从ID中获取用户对象

# In an event
@bot.event
async def on_ready():
await bot.wait_until_ready()
user = bot.get_user(431677941722906625) 
discrim = user.discriminator
# Do something

# In a command
@bot.command()
async def foo(ctx): # `!foo` in discord
user = bot.get_user(431677941722906625) 
discrim = user.discriminator

运行bot

bot.run("YOUR TOKEN HERE")

你的整个代码应该是这样的

import discord
from discord.ext import commands
intents = discord.Intents.default()
intents.members = True 
bot = commands.Bot(
command_prefix="!", # Change the prefix accordingly
intents = intents   # The intents we defined above
)
# In an event
@bot.event
async def on_ready():
await bot.wait_until_ready()
user = bot.get_user(431677941722906625) 
discrim = user.discriminator
# Do something

# In a command
@bot.command()
async def foo(ctx): # `!foo` in discord
user = bot.get_user(431677941722906625) 
discrim = user.discriminator

bot.run("YOUR TOKEN HERE")

看一下介绍和命令介绍

添加command_prefix

from discord.ext import commands
bot=commands.Bot(command_prefix='.')
user = bot.get_user(431677941722906625) 
discrim = user.discriminator

和不要忘记运行bot

from discord.ext import commands
bot = commands.Bot(command_prefix='.')
user = bot.get_user(431677941722906625)
discrim = user.discriminator
bot.run('Your Bots Token Here')

,您可以从这里获得bot的令牌https://discord.com/developers/application点击创建应用程序,转到Bot部分,点击创建Bot,然后复制Bot的令牌,将您的Bot令牌替换为您的Bot令牌

最新更新