Upload the file to the discord channel and get it (Because it was made by a beginner, there may be something strange)
python 3.8.2 discord.py 1.5.1
I wish I could save the data with discord Since it is running on heroku, it will restart automatically in about 24 hours. So when you save the file, it disappears The only way to use it is to rent a data server. I don't understand even if I look at various articles ... You should be able to upload files with discord here ... I created it like this
First of all, if you don't know what discord.py is, please refer to here. https://qiita.com/1ntegrale9/items/9d570ef8175cf178468f
First of all, it is assumed that the BOT setting etc. is completed and it can be used. Next, create a channel to save the data The name can be anything The next is the most important file to save Create the file with json This is the json file used this time
{
"goban":[
12345,
123456,
1234678
]
}
First, here is the code to read the file
@bot.event
async def on_ready():
mass = bot.get_channel(ID of the created channel)
id = mass.last_message_id
msg = await mass.fetch_message(id)
await msg.attachments[0].save("goban.json")
Here is the code to load and save Please change the name to save by yourself After that, open the file after the code of this and put it in list etc. You can use it when you put it in Please add according to the purpose of use
Here is the code to add to the file
@bot.command()
async def tui(ctx,ss):
global motolist
with open("goban.json", "r",encoding="utf-8") as moto:
moto = json.load(moto)
for da in (moto['goban']):
motolist += [f"{da}"]
First of all, if you try to add it to json, it will be overwritten. Then you can not use it, first open the file and add it to the list etc. The next important code to add
with open("goban.json","w",encoding="utf-8") as data:
data.write('{\n')
data.write('"goban":[\n')
for moto in motolist:
data.write(f'"{str(moto)}"')
data.write(", \n")
data.write(f'"{ss}"\n ]')
data.write('\n}')
await bot.get_channel(Created channel ID).send(file=discord.File('goban.json'))
If you connect this to the previous code, it will look like this
@bot.command()
async def tui(ctx,ss):
global motolist
with open("goban.json", "r",encoding="utf-8") as moto:
moto = json.load(moto)
for da in (moto['goban']):
motolist += [f"{da}"]
with open("goban.json","w",encoding="utf-8") as data:
data.write('{\n')
data.write('"goban":[\n')
for moto in motolist:
data.write(f'"{str(moto)}"')
data.write(", \n")
data.write(f'"{ss}"\n ]')
data.write('\n}')
await bot.get_channel(Created channel ID).send(file=discord.File('goban.json'))
Now you can add or freely add it You can update it by reopening it after this. If you connect all the cords so far
import discord
from discord.ext import commands
import json
bot = commands.Bot(command_prefix='.')
client = discord.Client()
motolist = []
@bot.event
async def on_ready():
mass = bot.get_channel()Created channel ID)
id = mass.last_message_id
msg = await mass.fetch_message(id)
await msg.attachments[0].save("goban.json")
@bot.command()
async def tui(ctx,ss):
global motolist
with open("goban.json", "r",encoding="utf-8") as moto:
moto = json.load(moto)
for da in (moto['goban']):
motolist += [f"{da}"]
with open("goban.json","w",encoding="utf-8") as data: #Su
data.write('{\n')
data.write('"goban":[\n')
for moto in motolist:
data.write(f'"{str(moto)}"')
data.write(", \n")
data.write(f'"{ss}"\n ]')
data.write('\n}')
await bot.get_channel(Created channel ID).send(file=discord.File('goban.json'))
motolist = []
bot.run("TOKEN")
This is a program used by python beginners. please tell me
Recommended Posts