【发布时间】: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,我们将不胜感激!
【问题讨论】: