Skip to content

Instantly share code, notes, and snippets.

@yonderbread
Last active October 25, 2020 21:02
Show Gist options
  • Save yonderbread/084bed60c0369aae756c836cfa6f25c6 to your computer and use it in GitHub Desktop.
Save yonderbread/084bed60c0369aae756c836cfa6f25c6 to your computer and use it in GitHub Desktop.
You can run this script to select DM or guild channels and send translated messages. (ONLY FOR SENDING MESSAGES NOT RECIEVING)
import requests
discord_base_url = 'https://discord.com/api/v8'
translation_base_url = 'https://api.mymemory.translated.net/get?q={0}&langpair={1}|{2}'
class Client:
def __init__(self, token):
self.token = token
self.selected_channel = None
self.input_lang = 'en'
self.out_lang = 'pt'
def get_header(self, token=None):
if not token:
token = self.token
return {'Authorization': token, 'Content-Type': 'application/json'}
@staticmethod
def response_ok(status_code):
return status_code in range(200, 204)
def get_channel_by_id(self, channel_id=None):
if not channel_id:
channel_id = self.selected_channel
header = self.get_header()
r = requests.get(f'{discord_base_url}/channels/{channel_id}', headers=header)
if self.response_ok(r.status_code):
data = r.json()
return data
return None
def send_message(self, message, channel_id=None):
if not channel_id:
channel_id = self.selected_channel
header = self.get_header()
r = requests.post(f'{discord_base_url}/channels/{channel_id}/messages',
headers=header, json={
"content": message,
"tts": False,
})
if self.response_ok(r.status_code):
return True
return False
def translate(self, text, from_lang='en', to_lang=None):
if not from_lang:
from_lang = self.input_lang
if not to_lang:
to_lang = self.out_lang
words = '+'.join(word for word in text.split())
r = requests.get(translation_base_url.format(words, from_lang, to_lang))
if r.status_code in range(200, 204):
data = r.json()
matches = []
best_match = None
for match in data['matches']:
if not best_match:
best_match = match
else:
if match['match'] > best_match['match']:
best_match = match
return best_match['translation']
return None
command_prefix = '!'
discord_token = ''
user_client = Client(token=discord_token)
running = True
if __name__ == '__main__':
while running:
cmd = input('> ')
args = cmd.split()
if len(args) >= 4:
if args[0] == command_prefix:
if args[1] == 'set':
if args[2] == 'channel':
channel_id = args[3]
channel = user_client.get_channel_by_id(channel_id)
if channel:
print('Selected Channel: ' + str(channel))
user_client.selected_channel = channel_id
else:
print('Could not find channel..')
elif args[2] == 'input':
user_client.input_lang = args[3].lower()
elif args[2] == 'output':
user_client.out_lang = args[3].lower()
else:
print('Invalid selection argumention.')
else:
message = ' '.join(w for w in args[1:])
translated_message = user_client.translate(message)
sent = user_client.send_message(translated_message)
if not sent:
print('Failed to send.')
else:
print('Sent.')
else:
message = ' '.join(w for w in args[1:])
translated_message = user_client.translate(message)
sent = user_client.send_message(translated_message)
if not sent:
print('Failed to send.')
else:
print('Sent.')
# Syntax!
**Select a channel**
```
! set channel CHANNEL_ID
```
**Select input language**
```
! set input LANG_CODE
```
**Select output language**
```
! set output LANG_CODE
```
**Yes, there is a space between the command prefix and first argument!**
**To send messages just type your message without the command prefix and hit enter!**
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment