【问题标题】:AttributeError: 'str' object has no attribute 'send' for my Discord botAttributeError:“str”对象没有我的 Discord 机器人的“send”属性
【发布时间】:2021-08-01 04:41:30
【问题描述】:
# bot.py
import os

import json
import discord
from discord import guild
from discord.ext import commands
from discord.utils import get
from dotenv import load_dotenv

load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')

client = discord.Client()
bot = commands.Bot(command_prefix='!')


def writeToJSON(path, fileName, data):
  filePathNameWExt = './' + path + '/' + fileName + '.json'
  with open(filePathNameWExt, 'w') as fp:
      json.dump(data, fp)


with open('config.json') as f:
  config = json.load(f)

path = './'
fileName = 'config'
data = {}


@bot.event
async def on_ready():
  print(f'{bot.user} has connected to Discord!')
  print(" {} is set as the server status channel.".format(config['statusChannel']))

  channel = config['statusChannel']
  await channel.send('This is the status channel.')


@bot.command()
async def setstatus(ctx, arg):
  await ctx.send('The server status channel has been set to ' + arg)
  newArg =""

  for character in arg:
      if character.isalnum():
          newArg += character

  data['statusChannel'] = newArg
  writeToJSON(path, fileName, data)


bot.run(TOKEN)

我只是想让机器人在我设置为服务器状态的频道中发送消息。我将频道 ID 保存在一个 json 文件中,并且可以打印该 ID,但似乎无法向该频道发送消息。另外,如果有更简洁的方法来保存频道 ID,我们将不胜感激!

【问题讨论】:

    标签: python discord


    【解决方案1】:

    JSON 文件中的我的频道 ID 是一个字符串。使用 int() 将其转换为 int。

    【讨论】:

      猜你喜欢
      • 2018-03-20
      • 2021-11-29
      • 1970-01-01
      • 2022-11-01
      • 1970-01-01
      • 2021-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多