2021年2月7日星期日

Discord.py channel and guild detection issues

Ive been looking for a way to save messages in discord channels onto a txt file (For practicing python).

Heres all my code right now:

import discord  import os    def createFolder(directory): #Thanks to keithweaver on github for file code!      try:          if not os.path.exists(directory):              os.makedirs(directory)      except OSError:          print ('Error: Creating directory. ' +  directory)    class MyClient(discord.Client):      async def on_message(self, message):          createFolder(message.guild.name)            encoded_string = message.channel.name.encode("ascii", "ignore")          decode_string = encoded_string.decode()          f = open(".\{}\{}.txt".format(message.guild.name, decode_string), "a") #Finds and or Creates a Txt file.            encoded_string = message.author.name.encode("ascii", "ignore") #Finds the NonAscii chars in a str.          decode_string = encoded_string.decode() #Takes the NonAscii chars out of the str.           f.write('\n' + 'Name: {} \n'.format(decode_string)) #Writes the str down on the Txt file.             encoded_string = message.content.encode("ascii", "ignore")          decode_string = encoded_string.decode()          f.write('Content: {} \n'.format(decode_string))            f.close() #Closes the current Txt file on the f variable.  client = MyClient()  client.run(My token, no you aren't getting it.)  

https://gist.github.com/keithweaver/562d3caa8650eefe7f84fa074e9ca949 heres the link to the github code I got from keithweaver.

It runs great but there is one problem...

If the Guild or Channels changes names then it will create a whole new folder and file. I don't want that and instead be able to detect these changes to avoid creating new files. I've thought about creating a new file to store the names and ids of the guild and channels along with a given ID number (Ex: 1, 2 ,3).

An example of how I would like the files to be arranged:

bot_name

Server1

Channel1
Channel2
Channel3
ChannelID's

Server2

Channel1
Channel2
Channel3
ChannelID's
ServerID's

This is how my files are currently arranged:

bot_name

server1

channel1
channel1ButNewName
channel2
channel3

server2

channel1
channel2
channel3

server1ButNewName

channel1
channel2
channel2ButNewName

Sorry if this is confusing but its the best way for me to explain this haha. But that is the way I want it to be structured, yet with the ServerID txt file I have to be able to write and read guild and channel ids from those files to be able to detect which file to write the message in.

I've tried already and cant seem to be able to write them down without creating more than 1 id for the same channel. This is where my question comes in (sorry for lengthy post)

I need a way to write messages from certain guilds/channels after they change names in the correct files.

If you need me to clarify on this please just ask, I'm not the best at explaining things the first time, thank you!!!

https://stackoverflow.com/questions/66094697/discord-py-channel-and-guild-detection-issues February 08, 2021 at 08:58AM

没有评论:

发表评论