# 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,将不胜感激!
发布于 2021-05-10 20:07:27
我在JSON文件中的频道ID是一个字符串。已使用int()
将其转换为int。
https://stackoverflow.com/questions/67476173
复制相似问题