discord.py(Python 3.6.5 Discord)每次尝试运行我的Discord bot时都会在'def'上说“无效语法”

时间:2018-06-12 07:23:56

标签: python-3.x

这是我的代码 -

import os, discord, asyncio, requests, threading, platform, requests, json, 
logging, random, pickle, time
from discord.ext import commands
from discord.ext.commands import Bot
from discord.utils import get


BOT_PREFIX = ("?", "!", "~", "`", "/", "-", "*", "^", "%", "$", "#", "&", "|")
TOKEN = "INSERT TOKEN"  # Get at discordapp.com/developers/applications/me

client = Bot(command_prefix=BOT_PREFIX)

@client.command(name = '8ball',
            brief="Answers a yes/no question.",
            description="Answers from the beyond.",
            aliases=['eight_ball', 'eightball', '8-ball'],
            pass_context=True)
async def eight_ball(context):
possible_responses = [
    'That is a resounding **no**',
    'It is **not** looking likely',
    'It is **quite** possible',
    '**Definitely**',
    'Definitely... **not**',
    '**Yes**',
    '**No way**',
    '**Of course!**',
    'I have spoken to the gods... They have said **Yes!** :smile:',
    'I have spoken to the gods... They have said **No!** :rage:',
    '**Maybe?**',
    '**Not at all!**',
    '**Not** in a **million** years.',
    '**You bet ;)**',

]
await client.say(random.choice(possible_responses) + ", " + context.message.author.mention)


@client.command(brief = 'Rolls 2 dice from 1-6',
            description = 'Rolls 2 dice',
            pass_context=True)
async def dice(context):
possible_responses = [
    '**2**',
    '**3**',
    '**4**',
    '**5**',
    '**6**',
    '**7**',
    '**8**',
    '**9**',
    '**10**',
    '**11**',
    '**12**',

]
await client.say(random.choice(possible_responses) + ", " + context.message.author.mention)

@client.command(name = 'dice3',
            category = 'Fun & Games',
            brief = 'Rolls 1 dice from 1-3',
            description = 'Rolls 1 dice',
            pass_context=True)
async def dice(context):
possible_responses = [
    '**1**',
    '**2**',
    '**3**',

]
await client.say(random.choice(possible_responses) + ", " + context.message.author.mention)

@client.command(brief = 'Deletes any number of messages you would like...',
            description = '!purge <number>',
            pass_context = 1) # enter ( ./purge <number> ) to delete a large num of messages.
async def purge(context, number : int):
    deleted = await client.purge_from(context.message.channel, limit = number)
    await client.send_message(context.message.channel, 'Deleted {} message(s)!'.format(len(deleted))


@client.event
async def on_message(message):
if message.content.startswith('!cool'):
    await client.send_message(message.channel, 'Who is **cool**? Type `!name` @(user)')

    def check(msg):
        return msg.content.startswith('!name')

    message = await client.wait_for_message(author=message.author, check=check)
    name = message.content[len('!name'):].strip()
    await client.send_message(message.channel, '{} is **cool** indeed'.format(name))

@client.event
async def on_message(message):
if message.content.startswith('!greet'):
    await client.send_message(message.channel, '**Say** `hello`')
    msg = await client.wait_for_message(author=message.author, content='hello')
    await client.send_message(message.channel, 'Hello.')

@client.command(category = 'Facts',
            brief = 'Shows the price of Bitcoin',
            description = '!bitcoin',
            pass_context=True)
async def bitcoin():
url = 'https://api.coindesk.com/v1/bpi/currentprice/BTC.json'
response = requests.get(url)
value = response.json()['bpi']['USD']['rate']
await client.say("**Bitcoin price is: $**" + value)

@client.event
async def on_message(message):
if message.content.startswith('!start'):
    await client.send_message(message.channel, 'Type `!stop` **4 times.**')
    for i in range(4):
        msg = await client.wait_for_message(author=message.author, content='!stop')
        fmt = '**{} left to go...**'
        await client.send_message(message.channel, fmt.format(3 - i))

    await client.send_message(message.channel, '**Good job!** :smiley:')


@client.event
async def on_ready():
await client.change_presence(game=Game(name="with nothing"))
print("Logged in as " + client.user.name)


async def list_servers():
await client.wait_until_ready()
while not client.is_closed:
    print("Current servers:")
    for server in client.servers:
        print(server.name)
    await asyncio.sleep(600)


client.loop.create_task(list_servers())
client.run("INSERT TOKEN")

有人可以帮助我,我不明白为什么会这样。我只是想和我的朋友玩得很开心,但这种情况一直在发生,我无法运行机器人。如果你能帮助我,那将非常感激:D。我是编码的新手,所以这可能是因为我可能找不到一些非常明显的错误。请帮帮我:)。

1 个答案:

答案 0 :(得分:0)

尝试从函数属性中删除上下文

async def eight_ball(context):

/

async def eight_ball():
  

v0.15.0新功能

     
    

对于命令扩展名,更改了以下内容:

Context is no longer slotted to facilitate setting dynamic attributes.