【问题标题】:Using Replit for a discord bot将 Replit 用于不和谐机器人
【发布时间】:2021-06-27 18:58:45
【问题描述】:

我正在制作一个 Groot discord 机器人,每次有人问它问题时它都会增长。每一百个问题,格鲁特就会上升一年。我正在使用 replit,但在编码方面我并没有那么先进。

import os
import requests
import json
import random



client = discord.Client()

i_am_groot = ("Hey Groot", "Groot", "Twig", "Tree")


client = discord.Client()

groot_age = 0
groot_pings = 100

@client.event
async def on_ready():
  print('We have logged in as {0.user}'.format(client))

@client.event
async def on_message(message):
  if message.author == client.user:
        return
  if message.content.startswith(i_am_groot):
        await message.channel.send('I am groot')
        groot_pings = groot_pings + 1
        groot_age = groot_pings // 100
        await client.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name='Mr Blue Sky and Dancing | Age: ' + str(groot_age) + ' | Pings: ' + str(groot_pings)))
  if message.content.startswith ('grootage'):
        await message.channel.send ('Translation: ' + str(groot_age))
  if message.content.startswith ('grootpings'):
        await message.channel.send ('Translation: ' + str(groot_pings))



import keep_alive

keep_alive.keep_alive()
    



client.run(os.getenv('TOKEN'))

我收到此错误:

[pyflakes] local variable 'groot_pings' defined in enclosing scope on line 19 referenced before assignment

但它定义的。

有没有办法解决这个问题?

帮助?

【问题讨论】:

标签: python discord discord.py bots


【解决方案1】:

试试这个代码:

import os
import requests
import json
import random



client = discord.Client()

i_am_groot = ("Hey Groot", "Groot", "Twig", "Tree")


client = discord.Client()

groot_age = 0
groot_pings = 100

@client.event
async def on_ready():
  print('We have logged in as {0.user}'.format(client))

@client.event
async def on_message(message):
  global groot_age
  global groot_pings
  if message.author == client.user:
        return
  if message.content.startswith(i_am_groot):
        await message.channel.send('I am groot')
        groot_pings = groot_pings + 1
        groot_age = groot_pings // 100
        await client.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name='Mr Blue Sky and Dancing | Age: ' + str(groot_age) + ' | Pings: ' + str(groot_pings)))
  if message.content.startswith ('grootage'):
        await message.channel.send ('Translation: ' + str(groot_age))
  if message.content.startswith ('grootpings'):
        await message.channel.send ('Translation: ' + str(groot_pings))



import keep_alive

keep_alive.keep_alive()
    



client.run(os.getenv('TOKEN'))

【讨论】:

    猜你喜欢
    • 2020-12-18
    • 2021-11-18
    • 2018-03-20
    • 2021-08-18
    • 2023-02-14
    • 2020-07-03
    • 2020-09-23
    • 2021-10-30
    • 2021-08-05
    相关资源
    最近更新 更多