角色是必需的参数,缺少

时间:2019-03-29 19:31:15

标签: python discord.py

遇到问题时,我正在研究discord.py,“角色是缺少的必需参数”

我试图删除ctx,但是后来我无法获得用户名或角色。

@client.command()
async def GiveRole(ctx, role):
    user = ctx.message.author
    await client.give_roles(user, role)
    await client.say(“Role %s given to %s.” %(role,user))

应该为您提供“角色”中指定的角色。

我的整个代码是:

from discord.ext.commands import Bot
import random
import discord
import operator
from discord.utils import get
import discord, datetime, time
from discord import Game
BOT_PREFIX = ("yaz ")
TOKEN = ("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
xp = {}

currentlink = "None"
currentmode = "None"
levels = {}
leader = ()
everyone = "off"
idname = {}
warns = {}
rewards = {}
xpstat = "on"
from discord.ext import commands
from discord.utils import get
client = Bot(command_prefix=BOT_PREFIX)
client.remove_command('help')


@client.command()
async def help(ctx):
   author = ctx.message.author
   embed = discord.Embed(colour = discord.Colour.red())
   embed.set_author(name='HELP!')
   embed.add_field(name='yaz setlink <link WITHOUT https> <mode>', value='Sets the current server link!', inline=False)
   embed.add_field(name='yaz help', value='DM them help message :D', inline=False)
   embed.add_field(name='yaz link', value='Shows the current server link!', inline=False)
   await client.send_message(author,embed=embed)


@client.command()
async def level(ctx):
   global xp
   person = ctx.message.author.id
   lev = xp[person]["level"]
   xptotal = xp[person]["fullxp"]
   embed = discord.Embed(colour = discord.Colour.green())
   embed.set_author(name='Level')
   embed.add_field(name='You are level %s.' %(lev),value='%s xp total.' %(xptotal),inline=False)
   await client.say(embed=embed)

@client.command()
async def addscore(ctx, score, mode, time, piclink):
   global lscore
   global xp
   global lmode
   lmode = mode
   lscore = score
   id = ctx.message.author.id
   rtime = time
   xp[id]["hightime"] = rtime
   xp[id]["highscore"] = lscore
   xp[id]["highmode"] = lmode
   xp[id]["highpic"] = piclink
   await client.say("Your score of %s is now on your profile!" %(lscore))

@client.command(pass_context=True)
async def fan(ctx):
   fanyay = ctx.message.author.name
   await client.say("Thank you, %s! That was nice of you!" %(fanyay))


@client.command()
@commands.has_any_role("Yazmania")
async def rep(ctx, addremove, userid, amount=1):
   global xp
   if addremove == "add":
       xp[userid]["rep"] += amount
       await client.say("Given %s %s rep. (Hope they were a real person!)" %(userid,amount))
   if addremove == "remove":
       xp[userid]["rep"] -= amount
       await client.say("Removed %s rep from %s. (Hope they were a real person!)" %(amount,userid))



@client.command()
async def profile(ctx):
   global xp
   name = ctx.message.author.name
   id = ctx.message.author.id
   score = xp[id]["highscore"]
   mode = xp[id]["highmode"]
   pic = xp[id]["highpic"]
   lev = xp[id]["level"]
   money = xp[id]["coins"]
   rep = xp[id]["rep"]
   time = xp[id]["hightime"]
   xptotal = xp[id]["fullxp"]
   if name != "Yazmania":
       embed = discord.Embed(colour = discord.Colour.green())
       embed.set_author(name='Profile:')
       embed.add_field(name='Hello, %s!' %(name), value='How may I help today, sir?', inline=False)
       embed.add_field(name='Highscore:', value='%s in %s. Looks like it took you %s to get that! Proof is %s.' %(score,mode,time,pic), inline=False)
       embed.add_field(name='Level:', value='You are level %s, with a total of %s xp!' %(lev, xptotal), inline=False)
       embed.add_field(name='Rep:', value='You have %s reputation, %s.' %(rep,name), inline=False)
       embed.add_field(name='Coins:', value='You have %s coin(s), %s.' %(money,name), inline=False)
       embed.add_field(name='Buy you a nice seafood dinner', value='and never call you again.', inline=False)
       await client.say(embed=embed)
   else:
       embed = discord.Embed(colour = discord.Colour.red())
       embed.set_author(name='God:')
       embed.add_field(name='Hello, YAZMANIA, THE OWNER!!!!!!', value='I BOW DOWN TO YOU YAZ :place_of_worship:', inline=False)
       embed.add_field(name='Highscore:', value='%s in %s. Looks like it took you %s to get that! Proof is %s. But you are YAZMANIA, so it does not really matter!' %(score,mode,time,pic), inline=False)
       embed.add_field(name='Level:', value='You are level ∞, with a total of ∞ xp! YAZMANIA :smile:', inline=False)
       embed.add_field(name='Rep:', value='You have ∞ reputation YAZMANIA because you are too cool to have less than ∞.', inline=False)
       embed.add_field(name='Coins:', value='You have %s coins, but you can just use `yaz give <id> <coins>`.' %(money), inline=False)
       embed.add_field(name='THANK YOU YAZMANIA', value='Subscribe to Yazmania!', inline=False)
       await client.say(embed=embed)


@client.command()
@commands.has_any_role("Yazmania")
async def clear(ctx, amount=10):
   channel = ctx.message.channel
   messages = []
   async for message in client.logs_from(channel, limit=int(amount)):
       messages.append(message)
   await client.delete_messages(messages)
   author = ctx.message.author
   await client.send_message(author,"Rip %s messages... you will be missed :sob:" %(amount))

@client.command()
async def game(game):
   await client.change_presence(game=Game(name="%s" %(game)))
   await client.say("I am now playing %s." %(game))


@client.command()
@commands.cooldown(1, 60*60*24, commands.BucketType.user)
async def daily(ctx):
   global xp
   coins = round(random.random() + 1 * 5)
   name = ctx.message.author.name
   id = ctx.message.author.id
   xp[id]["coins"] += coins
   await client.say("%s, you just collected your daily bonus of %s coins!" %(name,coins))


@client.command()
async def setlink(link, mode):
   global currentlink
   global currentmode
   global xp
   currentlink = "https://" + link
   currentmode = mode
   await client.send_message(destination=client.get_channel("529144661810479104"), content='The link for a %s game is %s' %(currentmode,currentlink))
   await client.say("The Current Server link has been set to %s with mode %s." %(currentlink,currentmode))

@client.command()
async def link():
   global currentlink
   global currentmode
   await client.say("Want to join a(n) %s game? Here's the link: %s" %(currentmode,currentlink))  

@client.command()
async def promotelink():
   global currentlink
   global currentmode
   await client.send_message(destination=client.get_channel("529138809246580747"), content='Join a %s game! %s' %(currentmode,currentlink))
   await client.say("Promotion complete.")


@client.command()
async def removelink():
   global currentlink
   global currentmode
   currentlink = "`error`"
   currentmode = "`error`"
   await client.say("Link deleted.") 

@client.event
async def on_message(message):
   global xpstat
   global idname
   global xp
   global leader
   global levels
   id = message.author.id
   msgname = message.author.name
   idname[id] = message.author.name
   if id != client.user.id and id in xp and xpstat != "off":
       xp[id]["xp"] = xp[id]["xp"] + 1
       xp[id]["fullxp"] = xp[id]["fullxp"] + 1
       print("%s by %s" %(message,id))
       if xp[id]["xp"] > xp[id]["wait"]:
           xp[id]["xp"] = 0
           xp[id]["wait"] *= 1.5
           xp[id]["level"] += 1
           levels[id] += 1
           await client.send_message(message.channel, "Level up %s! You are now level %s!" %(msgname,xp[id]["level"]))
           xp[id]["rep"] += 5
           print("%s just leveled up to level %s!" %(id, xp[id]["level"]))
   else:
       xp[id] = {"coins": 25,"xp": 0, "level": 1, "wait": 25, "fullxp": 0, "highscore": 0, "highmode": "please set a mode.", "highpic": "https://postimages.org", "rep": 100, "hightime": "0:00"}
       levels[id] = 0
   await client.process_commands(message)

@client.event
async def on_ready():
   await client.change_presence(game=Game(name="Yazmania is NEAT!"))
   print("YazBot is ready to go!")

client.run(TOKEN)

如果有人可以提供帮助,将不胜感激。以前从未发生过此错误。

1 个答案:

答案 0 :(得分:0)

您必须在Discord.py的async版本中传递上下文

@client.command(pass_context=True)
async def GiveRole(ctx, role:discord.Role):
    user = ctx.message.author
    await client.add_roles(ctx.message.author,role)
    await client.say(f"Role {role.name} give to {ctx.message.author.display_name}")

要使用角色的属性,必须首先通过执行role:discord.Role将其转换为角色对象。同样,通过不执行pass_context=True,您将无法使用上下文的属性,这意味着您将无法获得属性值,例如触发命令的人的用户名,它也应该是{{1 }},最后请查阅官方文档here