我的python代码拒绝为我正在制作的这个不一致的机器人运行

时间:2019-10-11 20:48:29

标签: python discord.py

什么都行不通

客户端根本无法运行,并且我得到一个空白的控制台,并且我无法使漫游器在不一致的情况下进行响应

我已经运行了漫游器,但拒绝运行 它已停止响应我在不和谐聊天中输入的文本,并且将不响应

import discord
from discord.ext import commands
import asyncio
from itertools import cycle

token = 'Dummy Token ACGT'
status = ['EpicGamerTime', 'Help', 'GamerStuff']
client = commands.Bot(command_prefix = ",")

@client.event   
async def on_ready(self):
    print('Logged in as')
    print(self.user.name)
    print(self.user.id)
    print('------')

async def change_status():
    await client.wait_until_ready()
    statuses = cycle(status)
    while not client.is_closed:
        current_status = next(statuses)
        await client.change_presence(activity=discord.Game(name=current_status))
        await asyncio.sleep(2) 

@client.event
async def on_message(self, message):
    # we do not want the bot to reply to itself
    if message.author.id == self.user.id:
        return

    if message.content.startswith('!hello'):
        await message.channel.send('Hello {0.author.mention}'.format(message))

    if message.content.startswith('!who is online'):
        await message.channel.send('Not Sure {0.author.mention}'.format(message))

    if message.content.endswith('whammy'):
        await message.channel.send('response {0.author.mention}'.format(message))



client.loop.create_task(change_status())
client.run(token)

没有输出或错误消息

2 个答案:

答案 0 :(得分:1)

您正在将self用作函数中的变量。通常在创建您不使用的类时使用。

从您的功能中删除self,它可以正常工作。

此外,我建议您重新生成机器人令牌。由于您已将其包含在问题中,因此任何人都可以使用它来劫持您的机器人。重新生成令牌,请勿与任何人共享。

import discord
from discord.ext import commands
import asyncio
from itertools import cycle

token = 'token'
status = ['EpicGamerTime', 'Help', 'GamerStuff']
client = commands.Bot(command_prefix = ",")

@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('------')

async def change_status():
    await client.wait_until_ready()
    statuses = cycle(status)
    while not client.is_closed:
        current_status = next(statuses)
        await client.change_presence(activity=discord.Game(name=current_status))
        await asyncio.sleep(2)

@client.event
async def on_message(message):
    # we do not want the bot to reply to itself
    if message.author.id == client.user.id:
        return

    if message.content.startswith('!hello'):
        await message.channel.send('Hello {0.author.mention}'.format(message))

    if message.content.startswith('!who is online'):
        await message.channel.send('Not Sure {0.author.mention}'.format(message))

    if message.content.endswith('whammy'):
        await message.channel.send('response {0.author.mention}'.format(message))



client.loop.create_task(change_status())
client.run(token)

答案 1 :(得分:0)

我从函数中删除了所有self,并在使用client的地方使用了self

希望这会有所帮助

import discord
import asyncio
from discord.ext import commands
from itertools import cycle
from toke import token

status = ['EpicGamerTime', 'Help', 'GamerStuff']
client = commands.Bot(command_prefix=",")


@client.event   
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('------')


async def change_status():
    await client.wait_until_ready()
    statuses = cycle(status)
    while not client.is_closed:
        current_status = next(statuses)
        await client.change_presence(activity=discord.Game(name=current_status))
        await asyncio.sleep(2) 


@client.event
async def on_message(message):
    # we do not want the bot to reply to itself
    if message.author.id == client.user.id:
        return

    if message.content.startswith('!hello'):
        await message.channel.send('Hello {0.author.mention}'.format(message))

    if message.content.startswith('!who is online'):
        await message.channel.send('Not Sure {0.author.mention}'.format(message))

    if message.content.endswith('whammy'):
        await message.channel.send('response {0.author.mention}'.format(message))


client.loop.create_task(change_status())
client.run(token)

顺便说一句,请不要发布您的令牌